简体   繁体   English

jQuery一次清除多个文本字段的值

[英]Jquery clearing value for multiple text fields at once

I have 4 text fields in a form that I want to clear when clicking a div. 我在单击div时要清除的表单中有4个文本字段。

I gave each text field a class class="1" and am using this function to clear them. 我给每个文本字段一个类class="1"并且正在使用此功能清除它们。

$('#clear').click(function() {
    $(".1").val('');
});

This seems to work but I'm wondering if there's a better, standard way to do it. 这似乎可行,但是我想知道是否存在更好的标准方法。

Note: I don't want to clear the entire form. 注意:我不想清除整个表格。

Aside from that being an invalid class name (pretty sure they can't start with a number), this should be fine. 除了提供一个无效的类名(确信它们不能以数字开头)之外,这应该没问题。 I would however name the class in a more meaningful way. 但是,我将以一种更有意义的方式为该班级命名。

Just be clear you shouldn't have css names start with a number to conform to the standard. 请清楚,您不应该让CSS名称以符合标准的数字开头。 But if you do manage to make that work you just might have to make your selector be slightly different and it should pick it up. 但是,如果您确实做到了这一点,则可能只需要使选择器稍有不同,就可以选择它。

$("#clear").click(function() {
    $("input[class='1']").val("");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM