脚本中的数字格式不正确 var合计=0; $(“.number”).focusout(函数(){ 如果(!isNaN(parseInt($(this.val())| | parseInt($(this.val())>=0){ Total+=parseInt($(this.val()); $(“.total”).text(总计); } }); 当我输入文本框“100hai”时,javascript take 100是一个数字。 以100计算 2.当我在文本框中输入asdas100时,可以进行验证。 如何处理1。问题
将html更改为脚本中的数字格式不正确 var合计=0; $(“.number”).focusout(函数(){ 如果(!isNaN(parseInt($(this.val())| | parseInt($(this.val())>=0){ Total+=parseInt($(this.val()); $(“.total”).text(总计); } }); 当我输入文本框“100hai”时,javascript take 100是一个数字。 以100计算 2.当我在文本框中输入asdas100时,可以进行验证。 如何处理1。问题,javascript,jquery,Javascript,Jquery,将html更改为 通过设置最小值,您可以确保use无法插入小于该值的值,如果可以解决您的问题,请尝试以下代码: <pre> <input type="number" value="0" class="number" name="amount1" min="1"> <input type="number" value="0" class="number" name="amount2" min="1"> <input type='text' v
通过设置最小值,您可以确保use无法插入小于该值的值,如果可以解决您的问题,请尝试以下代码:
<pre>
<input type="number" value="0" class="number" name="amount1" min="1">
<input type="number" value="0" class="number" name="amount2" min="1">
<input type='text' value='0' class='total' name='total'>
</pre>
$(“.number”)。按键((功能(e){
//如果字母不是数字,则显示错误,不键入任何内容
如果(e.which!=8&&e.which!=0&&e.which<48 | e.which>57)){
//显示错误消息
$(“#errmsg”).html(“仅限数字”).show().fadeOut(“慢”);
返回false;
}
否则{
Total+=parseInt($(this.val());
$(“.total”).text(total);}
不要使用parseInt()方法。使用Number(/your variable/)。使用
和此.valueAsNumber
而不是$(this.val()
<pre>
<input type="number" value="0" class="number" name="amount1" min="1">
<input type="number" value="0" class="number" name="amount2" min="1">
<input type='text' value='0' class='total' name='total'>
</pre>
$(".number").keypress((function (e) {
//if the letter is not digit then display error and don't type anything
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
//display error message
$("#errmsg").html("Digits Only").show().fadeOut("slow");
return false;
}
else {
Total+=parseInt($(this).val());
$(".total").text(Total); }