使用 jquery,我想根据类名选择器删除隐藏输入,但我只想在一个特定表单内删除它?
像这样:
$('.addToCartButton').click(function () {
var form = $(this).closest('form');
//remove all inputs with class name "test" inside of the form object above.
});
最好的方法是什么?
更新
<小时/>作为后续问题,我如何
1. 在删除每个输入 val() 之前先将其读入变量?
2. 从 find 方法中获取第一个元素(因为它可以返回多个元素)?
最佳答案
这样就可以了
$(form).find('input.test').remove(); //remove all elements
跟进:
var firstItem=$(form).find('input.test')[0]; // only the first item
var arr=[]; //this will build an array of the values
$(form).find('input.test').each(function(){
arr.push($(this).val());
});
var arr=[]; //this will build an array of the names andvalues with a comma between
$(form).find('input.test').each(function(){
arr.push($(this).name + "," + $(this).val());
});
循环遍历数组中的所有值
for(var i=0;i<arr.length;i++){
alert(arr[i]);
}
关于jquery - 如何从特定表单中删除字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7231680/