【问题标题】:Split array values in javascript+codeigniter在 javascript+codeigniter 中拆分数组值
【发布时间】:2013-09-19 01:58:13
【问题描述】:

查看文件:

<form name="sortdata" method="POST" action="<?php echo base_url;?>home/sortby">
<select id="sortMyData" name="sortMyData" class="sortMydata">
    <option value="lowhigh">Low to high price</option>
    <option value="highlow">High to low price</option>
</select>
<input type="hidden" id="baseurl" name="baseurl" value="<?php echo base_url;?>">
</form>

<div id="sortbyprice" class="span6">


</div>

<script type="text/javascript">
$(document).ready(function()
{
    $(".sortMydata").change(function()
{
var baseurl=$("#baseurl").val();
var sortMyData=$("#sortMyData").val();
//alert(baseurl);
alert(sortMyData);
$.ajax
({
type:"GET",
url:baseurl + 'product/sortbydetails/' + sortMyData,
//data:this.value,
cache: false,
success:function(html)
    { 
        var length=html.length;
        alert(html);
        alert(html[0]); 

    }
});
});
});
</script>

控制器:

public function sortbydetails()
    {
        $lowhigh=$this->uri->segment(3);
        if($lowhigh=="lowhigh")
        {
            $lowtohighprice=$this->product_model->getlowtohighprice();  
            $count=count($lowtohighprice);
            //echo $count;

            for ($i=0; $i < $count ; $i++) 
                { 

                    foreach ($lowtohighprice as $key => $value) {
                    $sortby[] = array_values($value);

                }
                    print_r($sortby);
            }
            //print_r($lowtohighprice);
        }
        else
        {
            $hightolowprice=$this->product_model->gethightolowprice();
            //print_r($hightolowprice);
        }

    }

我正在尝试从 javascript 中拆分数组值。我写了一个 jquery ajax 代码并将输出显示在一个 div 中。现在将输出作为数组值获取,我想拆分该数组值。我花了一整天的时间来完成这项任务,但我无法解决。

【问题讨论】:

  • 我们使用 split 函数从一个重复字符的字符串中创建一个数组。你到底要什么?
  • 您应该使用console.log() 而不是alert()。它更容易使用,并且输出将到达您已经显示在屏幕上的 JavaScript 控制台。

标签: javascript jquery ajax arrays codeigniter


【解决方案1】:

我无法得到你想要的。 您可以使用任何循环从数组中获取所有值。

var myarray = [1,2,3,4];
for(var i= 0; i< myarray.length; i++)
{
alert(myarray[i]);
}

编辑:

对于二维数组

var myarray = [[1,2,3,4], [5,6,7,8]];
for(var i= 0; i< myarray.length; i++)
  for(var j=0; j < myarray[i].length; j++)
     {
      alert(myarray[i][j]);
     }

【讨论】:

  • 我附上了我的输出截图。现在它是一种数组格式,我想从 javascript 中拆分这些数组值。
【解决方案2】:

在 ES6 中

你可以使用地图

var myarray = [1,2,3,4];

myarray.map(a=>alert(a));

它也返回值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2017-06-28
    • 1970-01-01
    • 2018-01-02
    • 2018-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多