如何使用 jQuery 清空输入字段

发布于 2025-01-04 03:27:25 字数 364 浏览 2 评论 0原文

我在移动应用程序中,使用输入字段来命令用户提交号码。

当我返回并返回到输入字段显示输入字段中显示的最新数字输入的页面时。

有没有办法在每次加载页面时清除该字段?

$('#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

油饼 2025-01-11 03:27:25

您可以使用 $('#shares').val(''); 清除输入字段

You can clear the input field by using $('#shares').val('');

荒芜了季节 2025-01-11 03:27:25
$(document).ready(function(){
  $('#shares').val('');
});
$(document).ready(function(){
  $('#shares').val('');
});
南…巷孤猫 2025-01-11 03:27:25

重置文本、数字、搜索、文本区域输入:

$('#shares').val('');

重置选择:

$('#select-box').prop('selectedIndex',0);

重置单选输入:

$('#radio-input').attr('checked',false);

重置文件输入:

$("#file-input").val(null);

To reset text, number, search, textarea inputs:

$('#shares').val('');

To reset select:

$('#select-box').prop('selectedIndex',0);

To reset radio input:

$('#radio-input').attr('checked',false);

To reset file input:

$("#file-input").val(null);
淡淡離愁欲言轉身 2025-01-11 03:27:25

提交表单时使用表单上的重置方法。 reset() 方法重置表单中所有元素的值。

$('#form-id')[0].reset();

OR 

document.getElementById("form-id").reset();

https://developer.mozilla.org/en-US/docs /Web/API/HTMLFormElement/重置

$("#submit-button").on("click", function(){
       //code here
       $('#form-id')[0].reset();
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<form id="form-id">
  First name:<br>
  <input type="text" name="firstname">
  <br>
  Last name:<br>
  <input type="text" name="lastname">
  <br><br>
  <input id="submit-button" type="submit" value="Submit">
</form> 
</body>
</html>

While submitting form use reset method on form. The reset() method resets the values of all elements in a form.

$('#form-id')[0].reset();

OR 

document.getElementById("form-id").reset();

https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/reset

$("#submit-button").on("click", function(){
       //code here
       $('#form-id')[0].reset();
});
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<form id="form-id">
  First name:<br>
  <input type="text" name="firstname">
  <br>
  Last name:<br>
  <input type="text" name="lastname">
  <br><br>
  <input id="submit-button" type="submit" value="Submit">
</form> 
</body>
</html>

就像说晚安 2025-01-11 03:27:25

设置 val('') 将清空输入字段。所以你可以使用这个:

页面加载时清除输入字段:

$(function(){
    $('#shares').val('');
});

Setting val('') will empty the input field. So you would use this:

Clear the input field when the page loads:

$(function(){
    $('#shares').val('');
});
幻梦 2025-01-11 03:27:25

既然您使用的是 jQuery,那么如何使用触发器-重置

$(document).ready(function(){
  $('#shares').trigger(':reset');
});

Since you are using jQuery, how about using a trigger-reset:

$(document).ready(function(){
  $('#shares').trigger(':reset');
});
岁月流歌 2025-01-11 03:27:25

如果您点击“后退”按钮,它通常会粘住,您可以做的就是在提交表单时清除该元素,然后在进入下一页之前但在处理该元素之后执行您需要的操作。

    $('#shares').keyup(function(){
                payment = 0;
                calcTotal();
                gtotal = ($('#shares').val() * 1) + payment;
                gtotal = gtotal.toFixed(2);
                $('#shares').val('');
                $("p.total").html("Total Payment: <strong>" + gtotal + "</strong>");
            });

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.

    $('#shares').keyup(function(){
                payment = 0;
                calcTotal();
                gtotal = ($('#shares').val() * 1) + payment;
                gtotal = gtotal.toFixed(2);
                $('#shares').val('');
                $("p.total").html("Total Payment: <strong>" + gtotal + "</strong>");
            });
〗斷ホ乔殘χμё〖 2025-01-11 03:27:25

如果输入是预先填写的(例如在提交的搜索表单中),您还必须清除值属性:

$("#clearSearch").on('click', function(){
  $('#search').val('').attr('value','');
});

If the input is pre-filled (e.g. in a submitted search form), you have to clear the value-attribute as well:

$("#clearSearch").on('click', function(){
  $('#search').val('').attr('value','');
});
无风消散 2025-01-11 03:27:25
$(function(){
    loadData(); 
    $(document).ready(function(){
  $('#insert').trigger(':reset');

  var item=$("#item").val(null);
  var qty=$("#quantity").val(null);
   var bb=$("#bb").val(null);
    var ee=$("#ee").val(null);

});
});

$(function(){
    loadData(); 
    $(document).ready(function(){
  $('#insert').trigger(':reset');

  var item=$("#item").val(null);
  var qty=$("#quantity").val(null);
   var bb=$("#bb").val(null);
    var ee=$("#ee").val(null);

});
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文