所有输入值的jQuery表单重置按钮
我正在使用一个表单和一个重置按钮,我可以重置所有输入的值,但它根本不起作用 当我使用Firefox控制台选项卡进行调试时,我在jQuery脚本的末尾看到一个错误所有输入值的jQuery表单重置按钮,jquery,html,Jquery,Html,我正在使用一个表单和一个重置按钮,我可以重置所有输入的值,但它根本不起作用 当我使用Firefox控制台选项卡进行调试时,我在jQuery脚本的末尾看到一个错误非法字符。有人能告诉我这里哪里不对吗 <!DOCTYPE HTML> <html lang="en-IN"> <head> <meta charset="UTF-8"> <title></title> <script type="text/javas
非法字符
。有人能告诉我这里哪里不对吗
<!DOCTYPE HTML>
<html lang="en-IN">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery1.7.2.js"></script>
</head>
<body>
<script type="text/javascript">
jQuery('#reset').click(function(){
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
});
</script>
<form id='myform'>
<input type='text' value='test' />
<select>
<option>One</option>
<option selected="true">Two</option>
</select>
<select multiple="true" size="5">
<option>One</option>
<option selected="true">Two</option>
</select>
<input type='button' id='reset' value='reset' />
</form>
</body>
</html>
jQuery(“#重置”)。单击(函数(){
$(':输入','#我的表格')
.not(“:按钮,:提交,:重置,:隐藏”)
.val(“”)
.removeAttr('checked')
.removeAttr(“选定”);
});
一个
两个
一个
两个
尝试以下操作:
$(function() {
$('#reset').click(function() {
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
});
});
您正在DOM准备就绪之前运行javascript<代码>$(函数(){})仅在DOM就绪时运行。请在此处阅读更多信息:难道
就足够了吗
<form>
<input type='reset' />
</form>
重置表单字段的另一个jQuery解决方案是:
另一个,更具体,指必填字段:
-jQuery解决方案
仅使用JavaScript:
仅使用HTML:
正如其中一个答案中所提到的,在大多数情况下,您不需要JavaScript来重置表单字段,它只需对按钮设置type=“reset”
,例如:
<button type="reset" value="Reset">Reset</button>
重置
下面的JQuery将重置所有文本和下拉列表
只需将#YourID替换为要重置的字段/输入的ID,对于多个输入,复制行并在适当的位置添加ID
$(".btn-reset").click(function () {
$(this).closest('form').find("input[type=text], textarea").val("");
$('#YourID').removeAttr('selected').find('option:eq(0)').prop('selected', true);
});
在这里工作很好,但在这里无法使用纯html和jQuery。您是否运行了上面的fiddle?您是否尝试了('everything',function(){format c:})上的
$('#reset')代码>@rahul..我想小提琴的代码是完全一样的。那为什么它不在这里工作呢?@newuser刚刚将你的代码粘贴到我的IDE中。在你的}末尾有某种不可见的字符代码>。如果您只是删除或粘贴我编写的代码,它应该可以工作。@sQVe。。。不..这里也有同样的问题。我已经粘贴了你的全部代码,但它不起作用。@newuser Works为我查找,从这里获取完整代码:5年后,你的答案对我有帮助。谢谢你。
document.getElementById('form').reset();
<button type="reset" value="Reset">Reset</button>
$(".btn-reset").click(function () {
$(this).closest('form').find("input[type=text], textarea").val("");
$('#YourID').removeAttr('selected').find('option:eq(0)').prop('selected', true);
});