除收音机/选择菜单值以外的清除表单输入字段?

时间:2014-10-24 09:44:22

标签: javascript jquery

我正在使用以下代码清除表单中的所有输入字段,除了2个选择菜单和一个单选按钮组。

但它清除所有价值观?如何忽略'#selct1', '#select2, '#radios2'并清除表单中的所有其他输入值?

$('.myinputs')
    .not('#selct1', '#select2, '#radios2')
    .val('')
    .removeAttr('checked')
    .removeAttr('selected');

4 个答案:

答案 0 :(得分:3)

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') ); 

同时检查this

答案 1 :(得分:0)

$(".myinputs").not('[type=radio],[type=checkbox],select').val('');

答案 2 :(得分:0)

这应该对你有用,它比你使用的代码多一点,但它有效。

$("#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

答案 3 :(得分:0)

在您不想检查的控件(您的收音机和选择控件)中添加一个类(本例中为“DontClear”)。然后,尝试以下功能:

$(".myInputs").each(function(){
   if ($(this).hasClass("DontClear"))
      continue;
   $(this).val("");
});
相关问题