javascript 输入类型 ="数字" 值在 html 中不更新是否正常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28955330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Input type ="number" Value does not update in html is this normal
提问by confusedMind
I have not used this before just now and noticed it does not update its value in html?
我之前没有使用过它,并注意到它没有更新它在 html 中的值?
Basically i have a Html Table with this in it , and user will update it to a quantity needed and Submit and i will take the Html parse it to get the things.
基本上我有一个带有这个的 Html 表,用户将它更新到所需的数量并提交,我将使用 Html 解析它以获取内容。
But in the html i see the value of input box is always 1, i it never updates itself
但是在 html 中我看到输入框的值总是 1,我从不更新自己
<input type="number" id="testNumber" value="1" min="1" max="100" />
回答by irysius
While yes, the markup indicates that the value is still 1, if this form were to be submitted, the displayed value of the number input would still get returned.
虽然是的,标记表明该值仍然是1,如果要提交此表单,仍然会返回数字输入的显示值。
You can verify this by running the following in your browser's console:
您可以通过在浏览器的控制台中运行以下命令来验证这一点:
var input = document.getElementById('testNumber');
input.value;
EDIT 1:
编辑 1:
If you want the value of the html to match the value of the dom element, assign it yourself, like so:
如果您希望 html 的值与 dom 元素的值匹配,请自行分配,如下所示:
input.setAttribute('value', input.value);
回答by Giwoo Gustavo Lee
This is just a sample. But it handles the INCREASE(+)/DECREASE(-) buttons clicked and updates the input value.
这只是一个示例。但它处理单击的 INCREASE(+)/DECREASE(-) 按钮并更新输入值。
$("input[type=number]").click(function(e) {
$(this).attr( 'value', $(this).val() );
});
回答by Yousef Altaf
if I understood this question right, you need to change the number input
on event.
如果我对这个问题的理解正确,您需要更改number input
on 事件。
here is you can try
这是你可以试试
$("#quantity").val(1);
$("#quantity").val(1);
on event it will change the #quantity
to 1 even you would see this on the browser.
在事件中,#quantity
即使您会在浏览器上看到它,它也会将 更改为 1。
回答by Indra
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
jQuery('#input1').keyup(function(){
jQuery("#testNumber").val(jQuery(this).val());
});
});
</script>
</head>
<body>
<form>
Change Value:
<input type="text" name="input1" id="input1">
<br/>
<br/>
result
<input type="text" id="testNumber" value="1" min="1" max="100" readonly="" />
</form>
</body>
</html>