Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery onchange函数不处理动态生成的select标记_Jquery_Html_Ajax - Fatal编程技术网

jQuery onchange函数不处理动态生成的select标记

jQuery onchange函数不处理动态生成的select标记,jquery,html,ajax,Jquery,Html,Ajax,在动态表行中,我有一个select标记。在通过Ajax的第一行中,我可以更改select标记值,但是在动态的第二行select标记中,如果我做了任何更改,它将反映在第一行select标记上,而不是在同一行上 我做错了什么 $(document.body).on('change','#carwash',function(){ var washid=$(this.val(); console.log(washid); 如果(washid){ $.ajax({ 键入:“POST”, url:“/Ma

在动态表行中,我有一个select标记。在通过Ajax的第一行中,我可以更改select标记值,但是在动态的第二行select标记中,如果我做了任何更改,它将反映在第一行select标记上,而不是在同一行上

我做错了什么

$(document.body).on('change','#carwash',function(){
var washid=$(this.val();
console.log(washid);
如果(washid){
$.ajax({
键入:“POST”,
url:“/Main/fetch_items”,
数据:“washid=”+washid,
数据类型:“html”,
成功:功能(响应){
控制台日志(响应);
$('#product').html(回复);
}
});
}
});
$(document.body).on('click','product',function()){
var product=$(this.val();
控制台日志(产品);
$.ajax({
键入:“POST”,
url:“/Main/fetch_-mesure”,
数据:“subid=”+产品,
数据类型:“html”,
成功:功能(测试){
控制台日志(测试);
$('单位').val(测试);
}
});
$.ajax({
键入:“POST”,
url:“/Main/fetch_limit”,
数据:“subid=”+产品,
数据类型:“html”,
成功:功能(测试){
控制台日志(测试);
$('数量').val(测试);
}
})
});

洗涤式
产品
单位
一次洗涤要求
行动
选择洗涤类型
?php foreach($washtype作为$row):?>

只需更改控制事件

 $(document.body).on('change', '.product', function() {
        var product = $(this).val();
        console.log(product);
        $.ajax({
            type: 'POST',
            url: '<?php echo base_url();?>/Main/fetch_mesure',
            data: 'subid=' + product,
            dataType: "html",
            success: function(test) {
                console.log(test);
                $('#unit').val(test);
            }
        });

        $.ajax({
            type: 'POST',
            url: '<?php echo base_url();?>/Main/fetch_limit',
            data: 'subid=' + product,
            dataType: "html",
            success: function(test) {
                console.log(test);

                $('#quantity').val(test);
            }
        })
    });
$(document.body).on('change','.product',function()){
var product=$(this.val();
控制台日志(产品);
$.ajax({
键入:“POST”,
url:“/Main/fetch_-mesure”,
数据:“subid=”+产品,
数据类型:“html”,
成功:功能(测试){
控制台日志(测试);
$('单位').val(测试);
}
});
$.ajax({
键入:“POST”,
url:“/Main/fetch_limit”,
数据:“subid=”+产品,
数据类型:“html”,
成功:功能(测试){
控制台日志(测试);
$('数量').val(测试);
}
})
});

您需要通过公共类更改id的
#carwash
&
#product
,因为id在同一文档中应该是唯一的,否则它将始终引用第一个元素(本例中第一行中的元素),请按类更改HTML代码中的id,如:

<select class="form-control custom-select carwash" name='washtype[]' required>
<select class="form-control custom-select product" name='product[]' required>

然后在JS代码中:

$(document.body).on('change', '.carwash', function() {
  var washid = $(this).val();
  var product = $(this).closest('tr').find('.product');

  if (washid) {
    $.ajax({
      type: 'POST',
      url: '<?php echo base_url();?>/Main/fetch_items',
      data: 'washid=' + washid,
      dataType: "html",
      success: function(response) {
        product.html(response);
      }
    });
  }
});
$(document.body).on('change','.carwash',function()){
var washid=$(this.val();
var product=$(this.nexist('tr').find('.product');
如果(washid){
$.ajax({
键入:“POST”,
url:“/Main/fetch_items”,
数据:“washid=”+washid,
数据类型:“html”,
成功:功能(响应){
html(响应);
}
});
}
});
  • 如果需要ID,则需要唯一的ID

  • 如果使用相对寻址,则不需要ID:

    var-washtype=$(this).closest(“tr”).find(“[name='washtype[]”)


  • 1.如果需要ID,则需要唯一ID 2。如果使用相对寻址,则不需要ID:
    var washtype=$(this).closest(“tr”).find(“[name='washtype[]”)]”)
    我已按照您的建议进行了更改,但如果我添加第二行并进行更改,它将反映在第一行上,不在同一行。我还是按照你的建议做了。我在第二行所做的更改应用于第一行而不是同一行。好的,请给我们展示一下你的HTML的实际结构,包括两行。