[英]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.