如何获取每个字段的id和值?JQuery
亲爱的飞越者: 我有一个关于获取特定字段的id和值的问题。现在,我有以下代码:如何获取每个字段的id和值?JQuery,jquery,button,input,textfield,Jquery,Button,Input,Textfield,亲爱的飞越者: 我有一个关于获取特定字段的id和值的问题。现在,我有以下代码: <div class="category_edit_form"> <input type = "text" name = "category_edit_text" class = "category_edit_text" id = "'.$category->category_id.'"> <input type = "button" class="category
<div class="category_edit_form">
<input type = "text" name = "category_edit_text" class = "category_edit_text" id = "'.$category->category_id.'">
<input type = "button" class="category_edit_button" value = "Wijzig">
</div>
这是要发出警报的数据:
echo $_POST['category_id']."en de naam is: ".$_POST['category_name'];
但每次单击任何按钮时,我只看到第一个字段的值和id。因此,当我点击第二个文本字段的按钮时,会弹出第一个字段的id和值,而不是第二个文本字段的id和值
如何解决此问题?使用:
jQuery(".category_edit_button").click( function() {
jQuery.ajax({
async: false,
url: nieuwsbrief.updatecategory,
type: 'POST',
data: { category_id: jQuery(this).parent().find('.category_edit_text').prop('id'), category_name: jQuery('.category_edit_text').val()},
success: function(data) {
alert(data);
}
});
});
使用:
您需要确定选择器的范围,以获取
。编辑\u类别\u文本
jQuery(".category_edit_button").click( function() {
var $parent = $(this).parent();
jQuery.ajax({
async: false,
url: nieuwsbrief.updatecategory,
type: 'POST',
data: { category_id: jQuery('.category_edit_text',$parent).prop('id'), category_name: jQuery('.category_edit_text',$parent).val()},
success: function(data) {
alert(data);
}
});
});
您需要确定选择器的范围,以获取
。编辑\u类别\u文本
jQuery(".category_edit_button").click( function() {
var $parent = $(this).parent();
jQuery.ajax({
async: false,
url: nieuwsbrief.updatecategory,
type: 'POST',
data: { category_id: jQuery('.category_edit_text',$parent).prop('id'), category_name: jQuery('.category_edit_text',$parent).val()},
success: function(data) {
alert(data);
}
});
});
使用以下替代方法:
jQuery('.category_edit_text').prop('id')
此代码:
jQuery(this).prev('.category_edit_text').prop('id')
或prevAll()
如果不直接使用上一个元素,请使用以下内容代替:
jQuery('.category_edit_text').prop('id')
此代码:
jQuery(this).prev('.category_edit_text').prop('id')
或prevAll()
如果不是直接指向上一个元素