简体   繁体   English

单击功能正常,但“打开更改”不适用于多重选择框

[英]CLICK function is OK but ON CHANGE is not working on multi selectbox

Here is my code of JQuery- 这是我的JQuery代码-

$(document).ready(function(){

    //code for validation
    $("#categoryid").on("change", function() {
        var categoryid = $(this).val();
        $.ajax({
           type: "POST",
           url: "<?php echo site_url('user/order/getItem') ?>",
           data: {'categoryid': categoryid},
           cache: false,
           success: function(data)
           {
              $("#itemid").html(data);
           } 
       });
    });

    $("#itemid").on("change", function() {
        var itemid = $(this).val();

        alert(itemid);

    });
});

The first one (#categoryid) is working well. 第一个(#categoryid)运行良好。 but the second one (#itemid) is not working. 但是第二个(#itemid)无法正常工作。 If i change the "change" to "click" it works but i am surprised why it is not working on "change" function. 如果我将“更改”更改为“单击”,则可以使用,但令我惊讶的是为什么它不能使用“更改”功能。

<div class="form-group">
   <label>Select Category:</label>
       <select class="form-control" id="categoryid" autofocus>
       <?php
       $url = site_url('user/category/showCategoryJson'); // path to your JSON file
       $data = file_get_contents($url); // put the contents of the file into a variable
       $results = json_decode($data,true); // decode the JSON feed
       foreach ($results[0] as $key => $cat) { 
       ?>
           <option value="<?php echo $cat['categoryid']; ?>"><?php echo $cat['categoryname']; ?></option>
       <?php } ?>
       </select>
 </div>
 <div class="form-group">
     <label>Select Item:</label>
     <select class="form-control" id="itemid">
     </select>

 </div>

Here is my controller- 这是我的控制器-

public function getItem(){
    $data = array(
        'categoryid' => $this->input->post('categoryid')
    );

    $this->load->model('user/order_model');
    $res = $this->order_model->selectItem($data);

    if($res){           
        foreach ($res as $result) {
            echo "<option value=". $result['itemid'].">".$result['itemname']."</option>";
        }
    }

}

and the output picture- 和输出图片- 在此处输入图片说明

try this code in your success ajax call 在成功的ajax调用中尝试此代码

              $.each(data,function(i,data){
                  $("#itemid").append($('<option>', {
                        value : data,                                       
                        text : data
                    }));    
             })

Can you try this? 你可以试试这个吗?

jQuery(document).on('change','#itemid',function(){

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM