简体   繁体   English

如何使用表中一行内选择字段的更改功能获取动态输入字段的ID?

[英]How to get the id of dynamic input field using change function of select field inside a row in a table?

The problem is, I cant select the right input id of dynamic field inside a row.问题是,我无法在一行中选择正确的动态字段输入 ID。

<script>
$('tbody').on('change', 'select',function(e) //Same row #itemDescription.
{
e.preventDefault();
var id = $(this).val();
$.ajax({
type:'get',
url:'{{ route("<add.findItem>") }}' ,
data:{id:id},
dataType:'json',//return data will be json
success:function(data)
{

//THIS IS THE PROBLEM, EVERY TIME THE SELECT CHANGES, IT WILL DISPLAY THE VALUE TO THE LAST CREATED INPUT FIELD NOT CURRENT INPUT FIELD INSIDE A ROW. 
FIELD.
$('#itemDescription_'+ count +'').val(data.itemDescription);
},
error:function()
{
alert("Error Occurred");
}
}
);
});
</script>

I expect to display the values to the correct index inside the row.我希望将值显示到行内的正确索引中。

Without seeing your DOM structure it's very hard to help you with this, but fundamentally this in your change callback will refer to the select the change occurred on.如果没有看到您的 DOM 结构,很难帮助您解决这个问题,但从根本上this ,您的change回调中的this将引用发生更改的select You can use jQuery's various traversal methods to find elements near that specific select .您可以使用 jQuery 的各种遍历方法来查找特定select附近的元素。

For instance, this would find the first input on the same row as the select by finding the closest enclosing tr element and then using find to look within it for the :first input:举例来说,这将找到第一个input为在同一行中select通过寻找最接近的封闭tr元素,然后使用find到内它的外观为:first输入:

$('tbody').on('change', 'select',function(e) {
    var input = $(this).closest("tr").find("input:first");
    // ...
});

Then in your ajax callback you'd use input.val(data.itemDescription);然后在你的 ajax 回调中你会使用input.val(data.itemDescription); . .

To get the value of the select inside your table row获取表格行中选择的值


$(document).on('change', 'tbody tr select', function(){ 
   var dynamicInputVal = $(this).closest('tr').find("input[id^='itemDescription_']").val(); //This line must exactly be here, not in any other function

  $.ajax({
    type:'get',
    url:'{{ route("add.findItem") }}' ,
    data:{id:dynamicInputVal},
    dataType:'json',//return data will be json
    success:function(data){
       alert(dynamicInputVal)
    },
    error:function(){
       alert("Error Occurred");
    }
  });//end ajax



})


Your question is not clear, where is the dynamic input你的问题不清楚,动态input在哪里

暂无
暂无

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

相关问题 如何在表字段中添加带有输入文本的表行 - How to add table row with input text inside the table field 如何修改表格行中的克隆字段表单值和id? - How to modify cloned field form value and id inside table row? 使用getElementById为动态ID设置输入字段 - Styling input field of dynamic ID using getElementById 如何使用jQuery获取动态表的输入字段值 - How to use jQuery to get Input Field Value of dynamic table 如何将表列&#39;转换为输入字段&#39;并获取输入字段值和id? - How to convert table column 'Into input field' and get input field value and id? 我怎样才能得到一个<select>字段根据来自另一个的输入动态更改<select>React 中的字段? - How can I get a <select> field to change dynamically based on the input from another <select> field in React? 如何使用javascript选择器获取表td内输入字段的值? - How to get the value of an input field inside the table td using javascript selector? 更改嵌套在克隆表中的输入/选择字段的名称属性,以进行动态表单输入 - Javascript - Change name attribute of input/select field nested in a cloned table for dynamic form entry - Javascript 如何从 angular 中的 Jquery function 中的输入字段获取数据? - How to get data from input field inside a Jquery function in angular? 如何选择整个页面,而不是里面的输入字段? - How to select the whole page, but not the input field inside?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM