如何使用 jQuery 清空输入字段
我在移动应用程序中,使用输入字段来命令用户提交号码。
当我返回并返回到输入字段显示输入字段中显示的最新数字输入的页面时。
有没有办法在每次加载页面时清除该字段?
$('#shares').keyup(function(){
payment = 0;
calcTotal();
gtotal = ($('#shares').val() * 1) + payment;
gtotal = gtotal.toFixed(2);
$("p.total").html("Total Payment: <strong>" + gtotal + "</strong>");
});
I am in a mobile app and I use an input field in order user submit a number.
When I go back and return to the page that input field present the latest number input displayed at the input field.
Is there any way to clear the field every time the page load?
$('#shares').keyup(function(){
payment = 0;
calcTotal();
gtotal = ($('#shares').val() * 1) + payment;
gtotal = gtotal.toFixed(2);
$("p.total").html("Total Payment: <strong>" + gtotal + "</strong>");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用
$('#shares').val('');
清除输入字段You can clear the input field by using
$('#shares').val('');
重置文本、数字、搜索、文本区域输入:
重置选择:
重置单选输入:
重置文件输入:
To reset text, number, search, textarea inputs:
To reset select:
To reset radio input:
To reset file input:
提交表单时使用表单上的重置方法。 reset() 方法重置表单中所有元素的值。
https://developer.mozilla.org/en-US/docs /Web/API/HTMLFormElement/重置
While submitting form use reset method on form. The reset() method resets the values of all elements in a form.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset
设置
val('')
将清空输入字段。所以你可以使用这个:页面加载时清除输入字段:
Setting
val('')
will empty the input field. So you would use this:Clear the input field when the page loads:
既然您使用的是 jQuery,那么如何使用触发器-重置:
Since you are using jQuery, how about using a trigger-reset:
如果您点击“后退”按钮,它通常会粘住,您可以做的就是在提交表单时清除该元素,然后在进入下一页之前但在处理该元素之后执行您需要的操作。
if you hit the "back" button it usually tends to stick, what you can do is when the form is submitted clear the element then before it goes to the next page but after doing with the element what you need to.
如果输入是预先填写的(例如在提交的搜索表单中),您还必须清除值属性:
If the input is pre-filled (e.g. in a submitted search form), you have to clear the value-attribute as well: