[英]Clear form input fields except radio/select menu values?
我正在使用以下代碼清除表單中除2個選擇菜單和一個單選按鈕組之外的所有輸入字段。
但這會清除所有價值嗎? 如何忽略'#selct1', '#select2, '#radios2'
並清除表單中的所有其他輸入值?
$('.myinputs')
.not('#selct1', '#select2, '#radios2')
.val('')
.removeAttr('checked')
.removeAttr('selected');
val('')
將清除元素的內容。 您不需要.removeAttr('selected')
作為。
// clear input except radio buttons and < select >
$(".myinputs").not('[type=radio],[type=checkbox],select').val('');
// if yo need to reset select menu back to its default value
$('.selectClass').val( $('.selectClass').prop('defaultSelected') );
也檢查一下
$(".myinputs").not('[type=radio],[type=checkbox],select').val('');
這應該對您有用,它的代碼比您正在使用的要多,但它可以工作。
$("#btn").on("click", function(){
$(".myinputs").each(function(){
if($(this).attr("id") == ("radio1")){
return true;
}else if($(this).attr("id") == ("select1")){
return true;
}else if ($(this).attr("id") == ("select2")){
return true;
}else{
$(this).val("");
$(this).val([]);
}
});
});
這里是一個的jsfiddle它在行動
將一個類(在此示例中為“ DontClear”)添加到您不想檢查的控件(您的單選並選擇控件)中。 然后,嘗試以下功能:
$(".myInputs").each(function(){
if ($(this).hasClass("DontClear"))
continue;
$(this).val("");
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.