jquery - 如何从特定表单中删除字段

标签 jquery forms selector

使用 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

http://api.jquery.com/remove/

跟进:

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/

相关文章:

javascript - 是否有可能访问 JSON 对象中列出的 jQuery 选择器

forms - 为什么 Laravel 5.4 中表单不提交

javascript - XPath 可以用来搜索 &lt;script&gt; block 吗?

jquery - 我正在为特殊问题寻找 jQuery 或 CSS 选择器

objective-c - 取消排队的 performSelector :afterDelay calls

php - 如何从ajax数据发送上传pdf到服务器(使用jsPDF)

javascript - 如何用ExtJs ajax responseText替换元素

javascript - 通过Javascript在另一个html页面中显示HTML表单

forms - JQuery 表单验证不适用于新创建的元素

javascript - ScrollMagic:如何将内容放置在视差背景之上