JQuery 如何清除表单上 .class 的所有 input:textfields。不适合我

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8054377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 09:56:37  来源:igfitidea点击:

JQuery how to clear all the input:textfields of a .class on a form. Not working for me

jqueryforms

提问by Monty

I have a form, the first .class has 5 input:text boxes in it. I want to be able to clear all the text boxes with the click of a button. I've tried it a few ways and it's not working... here's what I've done.... (FYI, I have more classes in the form so I can't use .reset.

我有一个表单,第一个 .class 有 5 个 input:text 框。我希望能够通过单击按钮清除所有文本框。我已经尝试了几种方法,但它不起作用......这就是我所做的......(仅供参考,我在表单中有更多的类,所以我不能使用.reset。

The Form...

表格...

<table class="orderLine1 formFont" width="900">
 <tr>
    <td width="395">Product Name:<br><input class="ProductName" type="text" size="65"></td>
    <td width="97" class="formFontDisabled">Prod ID:<br><input class="Product_ID ffd" type="text" size="5" disabled></td>
    <td width="78" class="formFontDisabled">UPC:<br><input class="UPC ffd" type="text" size="13" disabled ></td>
    <td width="67" class="formFontDisabled">List Price:<br><input class="ListPrice ffd" type="text" size="7" disabled></td>    
    <td width="67">WholeSale:<br><input class="WholeSalePrice ffd" type="text" size="7" disabled></td>
    <td width="56">Quantity:<br><input class="qty addLine" type="text" size="7"></td>
    <td width="60" class="formFontDisabled">Line Total:<br><input class="subTotal ffd" type="text" size="10" disabled></td>
    <td width="44"><button class="OFDeleteButton">Delete</button><button class="OFClearLine" >OFClearLine</button></td>
  </tr>
</table>

The Script Test # 1

脚本测试 #1

   $(document).ready(function(e) {
    $(function clearFirst() {
         $('.orderLine1').find(':input').each(function() {
             switch(this.type) {
                 case 'text':
                    $(this).val('');
                    break;
                 }
             });
        });

    $('.OFClearLine').click(function(e) {
        clearFirst();
    });
    });

The Script Test # 2

脚本测试#2

  $(function(){
        $('.OFClearLine').bind('click', function(){
            $('.orderLine1').reset();
        });
    });

More info: OFClearLine is the class for the button I want to use & orderLine1 is the class of the form.

更多信息: OFClearLine 是我想使用的按钮的类 & orderLine1 是表单的类。

Thanks in advance!

提前致谢!

回答by PeeHaa

$(function() {
  $('.OFClearLine').click(function() {
    $('.orderLine1 input[type="text"]').val('');
  });
});

回答by James Johnson

Something like this should work:

这样的事情应该工作:

$("input.className:text").val("");

Looking at your code, this might work better:

查看您的代码,这可能会更好:

$(".className input[type='text']").val("");

Here's a jsFiddle: http://jsfiddle.net/nvUsD/1/

这是一个 jsFiddle:http: //jsfiddle.net/nvUsD/1/

回答by Jasper

The following code will find all the text inputs that are decendants of the .orderLine1table element and set their values to nothing.

下面的代码将查找作为.orderLine1表元素后代的所有文本输入,并将它们的值设置为空。

$('.OFClearLine').bind('click', function(){
    $('.orderLine1').find('input[type="text"]').val('');
});

回答by nickytonline

Try this:

尝试这个:

$(document).ready(function(e) {
    $('.OFClearLine').click(function(e) {
        $('.orderLine1 input:text').each(function() {
                $(this).val('');
        });
    });
});

You can see it in action on this fiddle, http://jsfiddle.net/nickyt/exWKL

你可以在这个小提琴上看到它的作用,http://jsfiddle.net/nickyt/exWKL

回答by Marklar

I'm not sure if it can see your clearFirst function. Also you can just use $('.orderLine1 input') to find all of the inputs in the .orderLine1 class.

我不确定它是否可以看到您的 clearFirst 函数。您也可以使用 $('.orderLine1 input') 来查找 .orderLine1 类中的所有输入。

$(document).ready(function(e) {    
    $('.OFClearLine').click(function(e) {
        clearFirst();
    });
});

function clearFirst() {
    $('.orderLine1 input[type="text"]').each(function() {
         $(this).val('');
    });
}