【问题标题】:How to make JSON data dynamic to get the value after selecting the first dropdown选择第一个下拉列表后如何使JSON数据动态获取值
【发布时间】:2017-06-23 06:33:49
【问题描述】:

从一个选项中选择选项后,我从查询中获得价值 表,它填充来自 ajax 的数据并返回一个 JSON 数据。问题是我正在动态接收 JSON 中的值,但我是 只传递第一个值所以..我怎样才能让这个动态成为我的 结果是要赋予价值的东西以及使其充满活力的关键。

    <script type="text/javascript">
    $(document).ready(function(){
        $('#cate').change(function(e){
            e.preventDefault();
            var cate=$( "#cate option:selected").val();
            var url="<?php echo base_url();?>cat_cntrl/subcatselect";
            $.ajax({
                type:'post',
                url:url,
                data:{cate :cate},
                dataType:"JSON",
                success:function(response){
                    console.log(response);
                    if (response.result ) {
                        $(response.result).each(function(k, v){
                            $('#myselect').append($('<option>', {
                              value: v['Mobile'],
                              text: v['Mobile']
                            }));
                        })
                    }

                }
            })
        })
    })
    </script>

这里是我的控制器代码:

<?php
class Cat_cntrl extends CI_controller{
    public function __construct(){
        parent::__construct();
        $this->load->model('cat_model');
        $this->load->helper('form','url');
    }

    public function index(){
        $get=$this->cat_model->fetchcatdata();
        $data['result']=$get;
        $this->load->view('cat_view',$data);
    }
    public function subcatselect(){
        $cate=$this->input->post('cate');
        $get=$this->cat_model->getsubo($cate);
        $data['result']=$get;
        echo json_encode(array('result'=>$data['result']));
    }
}
?>

【问题讨论】:

  • 你能把控制器功能的代码放上去吗?以便我们可以看到您返回到 ajax 调用的内容?
  • 这里不用preventDefault(),用var cate=this.value;代替var cate=$( "#cate option:selected").val();。我想,你的问题对 SO 用户来说不太清楚,你能详细解释一下吗?
  • 是否,$('#myselect') 不会在更改下拉列表时填充#cate
  • 我已经替换了这个但是它不起作用..
  • 解释一下。真的很难理解。你要说什么

标签: jquery json ajax codeigniter


【解决方案1】:

你可以像这样访问keydynamically

var json = JSON.parse('{"result":[{"car":"bmw"},{"car":"xuv"}]}');

   // console.log(json.result);

$.each(json.result,function(keys,values){
        for (var key in values) {
         console.log(values[key]);
         
         }
});
&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;

ajax : : 你的ajax成功函数应该是这样的

      .........
     if (response.result ) {
    $('#myselect').empty();
    $.each(response.result,function(keys, values){

        for (var key in values) {

         $('#myselect').append($('<option>', {
              value: values[key],
              text: values[key]
         }));

         }

    })
}
 .........

【讨论】:

    【解决方案2】:

    当您更改其父类别并更改使用$.each() 而不使用特定键时,您必须清除您的mySelect 下拉列表,

    if (response.result.length) {
        $('#myselect').empty();// make it empty if new child category is found
        $(response.result).each(function(key, obj) {
          $.each(obj, function(k, v){ 
            $('#myselect').append($('<option>', {
              value: v,
              text: v
            }));
          }); // end each obj
        }); // end each response.result
    }
    

    片段,

    var result = {
      "car": [{
        "car": "BMW"
      }, {
        "car": "Audi"
      }],
      "mobile": [{
        "mobile": "HTC"
      }, {
        "mobile": "Samsung"
      }]
    }
    
    function changeCat(result) {
      $('#myselect').empty(); // make it empty if new child category is found
      result.length && $(result).each(function(key, obj) {
          $.each(obj, function(k, v){
             $('#myselect').append($('<option>', {
                value: v,
                text: v
             }));
          });
      });
    }
    $('button[data-type]').on('click', function() {
      changeCat(result[$(this).attr('data-type')]);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select id="myselect"></select><br/>
    <button data-type="car">Select Car</button><br/>
    <button data-type="mobile">Select Mobile</button><br/>

    【讨论】:

    • 你没有明白我的意思,我得到了正确的 json,但是如果 v['mobile] 没有出现,我想让移动变为动态,它显示为空白,所以我希望它动态
    • if(v['Mobile']==TRUE){ value: v['Mobile'], text: v['Mobile'] } if(v['car']==TRUE ){ value: v['car'], text: v['car'] 我可以这样使用吗??
    • 如果您有数百个类别怎么办?如果您使用我的答案,那么您不必担心类别的数量。但条件是您必须以category:value 格式传递响应。
    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2016-07-20
    相关资源
    最近更新 更多