[英]Multiple input value on form submit
<input type="text" name="Amount" value="100" size="20" maxlength="10" id="textfield7" class="required">
如果我点击提交按钮我想要将值乘以100.有没有办法通过javascript做到这一点???
<script type="text/javascript">
function multiply(){
var val= document.getElementById("textfield7").value;
var newval= val*100;
alert(newval);
}
</script>
<input type="text" name="Amount" value="100" size="20" maxlength="10" id="textfield7" class="required" />
<input type="submit" onClick="multiply();"/>
I would simply write the correct value already multiplied by 100 in the input field. 我只需在输入字段中编写正确的值乘以100即可 。 :)
:)
If for any reason this can't be done I would simply do: 如果出于任何原因无法做到这一点,我只会这样做:
<form onsubmit="this[0].value*=100;">
<input type="text" name="Amount" value="100" size="20" maxlength="10" id="textfield7" class="required">
<input type="submit">
</form>
var submitButton = document.getElementById("submitButtonId");
submitButton.onclick = function (e) {
var inputAmount = document.getElementById("textfield7");
var calculatedAmount = parseInt(inputAmount.value, 10) * 100;
// do what you wish with calucatedAmount, such as returning false if you don't want to submit
}
Short: 短:
<form onSubmit="document.getElementById('textfield7').value *= 100;">
Cleaner: 清洁器:
<script type="text/javascript">
function multiply(){
var field = document.getElementById('textfield7');
field.value = parseInt(field.value) * 100;
}
</script>
<form onSubmit="multiply();">
Yes, but under any normal circumstances it would be the wrong way. 是的,但在任何正常情况下,这都是错误的方式。 Do the math in the server-side code.
在服务器端代码中进行数学运算。 It is pointless to take even a small risk of getting wrong data (when some data arrives without having been processed by client-side JavaScript).
即使获得错误数据的风险很小也是毫无意义的(当某些数据未经客户端JavaScript处理时到达)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.