jquery如何清空输入字段

时间:2022-11-13 23:00:29

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>");
});

6 个解决方案

#1


306  

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

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

#2


17  

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

#3


6  

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

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

Clear the input field when the page loads:

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

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

#4


2  

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

既然您正在使用jQuery,那么使用trigger-reset:

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

#5


2  

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

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>

#6


0  

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>");
            });

#1


306  

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

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

#2


17  

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

#3


6  

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

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

Clear the input field when the page loads:

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

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

#4


2  

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

既然您正在使用jQuery,那么使用trigger-reset:

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

#5


2  

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

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>

#6


0  

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>");
            });