Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.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
如何在Javascript/Jquery中添加表行时更改组合框选择上的文本框_Javascript_Jquery_Html - Fatal编程技术网

如何在Javascript/Jquery中添加表行时更改组合框选择上的文本框

如何在Javascript/Jquery中添加表行时更改组合框选择上的文本框,javascript,jquery,html,Javascript,Jquery,Html,我有一个动态添加行的表,它有一个选择框,当onchange函数被激活时,它会更改textbox的值,但问题是当添加新行时,它会重置所有textbox的值,而不是特定的空值。如何使其相应地更改 $(函数(){ $(文档).on('change','select.products',function(){ 所选变量=$(this.val(); $(“.price”).val(选定); }); $(文档).on('click','addProduct',function()){ var ele=$(

我有一个动态添加行的表,它有一个选择框,当onchange函数被激活时,它会更改textbox的值,但问题是当添加新行时,它会重置所有textbox的值,而不是特定的空值。如何使其相应地更改

$(函数(){
$(文档).on('change','select.products',function(){
所选变量=$(this.val();
$(“.price”).val(选定);
});
$(文档).on('click','addProduct',function()){
var ele=$(this.parents('tr').clone();
ele.find('input[type=text]').val('';
$(this.parents('tr')。在(ele)之后;
});
$(文档).on('单击','.delProduct',函数(){
if($(this).parents('table').find('tr').length>2){
$(this.parents('tr').remove();
}
});
});

POI
量
价格
产品
添加行?
1.
产品1
产品2
产品3

我已经浏览了您的JS FIDLE链接。您需要通过使用jquery获取当前列引用来将值更改为特定行

解决方案是,更改以下代码并尝试:

$(函数(){
$(文档).on('change','select.products',function(){
所选变量=$(this.val();
//$(“.price”).val(选定);
$(this).closest('tr').find('price').val(选中);
});
$(文档).on('click','addProduct',function()){
var ele=$(this.parents('tr').clone();
ele.find('input[type=text]').val('';
$(this.parents('tr')。在(ele)之后;
});
$(文档).on('单击','.delProduct',函数(){
if($(this).parents('table').find('tr').length>2){
$(this.parents('tr').remove();
}
});
});

POI
量
价格
产品
添加行?
1.
产品1
产品2
产品3

我已经浏览了您的JS FIDLE链接。您需要通过使用jquery获取当前列引用来将值更改为特定行

解决方案是,更改以下代码并尝试:

$(函数(){
$(文档).on('change','select.products',function(){
所选变量=$(this.val();
//$(“.price”).val(选定);
$(this).closest('tr').find('price').val(选中);
});
$(文档).on('click','addProduct',function()){
var ele=$(this.parents('tr').clone();
ele.find('input[type=text]').val('';
$(this.parents('tr')。在(ele)之后;
});
$(文档).on('单击','.delProduct',函数(){
if($(this).parents('table').find('tr').length>2){
$(this.parents('tr').remove();
}
});
});

POI
量
价格
产品
添加行?
1.
产品1
产品2
产品3

更好的解决方案是使用与下拉选项id/name匹配的不同id for price文本框生成整个tr,然后根据此id进行更新

如果您需要快速解决方案,请将您的更改事件替换为以下内容

   $(document).on('change', 'select.products', function(event){
        var selected = $(this).val();
        $(event.target.parentNode.parentNode).find(".price")[0].value = selected;
      });

更好的解决方案是使用与下拉选项id/name匹配的不同id for price文本框生成整个tr,然后根据该id进行更新

如果您需要快速解决方案,请将您的更改事件替换为以下内容

   $(document).on('change', 'select.products', function(event){
        var selected = $(this).val();
        $(event.target.parentNode.parentNode).find(".price")[0].value = selected;
      });

尝试运行代码段并检查…问题已解决。请为答案给我评分…谢谢。我该怎么做?单击向上投票图标。您可以在答案的左侧看到该图标(向上和向下箭头)。只需单击向上箭头。谢谢。尝试运行代码段并检查…问题已解决。为答案给我评分…谢谢。我该怎么做?单击向上投票图标。您可以在答案左侧看到该图标(向上和向下箭头)。只需点击向上箭头。谢谢。