2014-02-26 140 views
-2
$(':input','#myform') 
    .not(':button, :submit, :reset, :hidden') 
    .val('') 
    .removeAttr('checked') 
    .removeAttr('selected'); 

如何僅將它用於特定字段ID,而不是用於表單中的所有輸入?清除輸入。特定字段

<input type="text" name="example" id="id_example"/> 
<input type="text" name="example1" id="id_example1"/> 

回答

0

所以,只需使用id選擇器,並使用.val('')刪除輸入文本的輸入。

$('#id_example,#id_example1').val(''); 
1

使用Multiple Selector (「selector1, selector2, selectorN」)ID Selector (「#id」)

$(':input#id_example, :input#id_example1','#myform') 

的ID應該是獨特你可以只使用IDS

$('#id_example, #id_example1','#myform') 

您可以指定任意數量的選擇的結合成單一的 結果。這個多重表達組合子是一種有效的方式來選擇不同的元素。 返回的jQuery對象中的DOM元素的順序可能不相同,因爲它們將在 文檔順序中,jQuery doc

0
$(':input#id','#myform') 
.not(':button, :submit, :reset, :hidden') 
.val('') 
.removeAttr('checked') 
.removeAttr('selected'); 
0

你可以使用選擇太 '開頭':

$('#myform').find('[id^=id_example]').val(''); 
0

:input選擇所有的表單元素是否是一個input[type=text], input[type=radio], input[type=checkbox] input[type=button], input[type=submit], input[type=reset] etc. or select, textarea etc.

試試這個:

$('#myform').find(':text').val('').addBack(':checkbox').prop('checked', false); 

這清除了所有文本輸入的值,並取消選中複選框。