1

单击时要清除所有(不重置)输入(还清除默认文本框值)。需要使用尽可能少的资源的非常简短的代码。

HTML 表单

<form id="form2" action='project_manager' method='post'>
<input type='text' name='project' value="default value" >
<select name='job_number'>
<option value='0'></option>
<option value='1'>job1</option>
</select>

<input type="button" id="clear_all" value="Clear all" />
</form>

jQuery

$('input[id="clear_all"]').click (function () {
$(':input').val('');

});
});

但这也清除了按钮的价值。按钮的值必须保留。

变成$('form :input[type=text]').val("");

如何重置下拉菜单值?

试过$('form :select').val("");$('form :select').selectedIndex = -1;。有什么不对...

4

9 回答 9

4

尝试这个

$('form select').val("");
于 2013-08-28T04:30:32.273 回答
4
$(':input').not(":button").val('');

参考

于 2013-08-28T04:31:58.560 回答
1

这是清除表单内所有字段的代码。

$('#myform').find('input:text, input:password, input:file, select, textarea').val('');
$('#myform').find('input:radio, input:checkbox').removeAttr('checked').removeAttr('selected');
于 2013-08-28T04:42:30.987 回答
1

尝试

document.getElementById('formId').reset();
于 2014-01-07T08:26:44.427 回答
1

尝试input type="reset"

<input type="reset" id="clear_all" value="Clear all" />

你不需要写一行代码。

于 2013-08-28T04:32:18.743 回答
0
$(':input:not([type=button])').val('');
于 2013-08-28T04:32:23.690 回答
0

尝试这个

$(document).ready(function() { 
  $("#button_clear").click(function() { 
          document.getElementById('formId').reset(); 
    }); 
 });
于 2013-08-28T04:32:29.847 回答
0

更改按钮类型以重置

 <input type="reset" id="clear_all" value="Clear all" />

不需要 jquery 值将被清除

于 2013-08-28T04:33:51.087 回答
-1
try this.
 $('select').val(" ");
于 2013-08-28T05:11:08.027 回答