[英]How to use onchange event?
This is my code where if i put quantity then it will calculate it and show it in total amount. 这是我的代码,如果我输入数量,它将进行计算并显示总量。 my problem is that if i change my quantity in between process my total amount is not change. 我的问题是,如果我在两次处理之间更改我的数量,则我的总金额不变。
How to do it onchange
quantity total amount have to different 如何做到这一点onchange
量总量有不同
<script> function valid(obj) { var frm=document.getElementById("frm"); var qty=parseInt(document.getElementById("quantity").value); var price=parseInt(obj.value); var total=parseInt(document.getElementById("total").value); if(isNaN(total)) { total=0; } if(obj.checked==true) { total=total+(qty*price); document.getElementById("total").value=total; } else { total=total-(qty*price); document.getElementById("total").value=total; } /*for(var i=0;i<frm.length;i++) { if(frm[i].type=="checkbox") { if(frm[i].checked==true) { total=total+(parseInt(frm[i].value)*qty); } } }*/ document.getElementById("total").value=total } </script>
<form action="abc.html" method="post" id="frm" onsubmit="return valid(this);"> <table> <tr> <th colspan="2">Qualification</th> </tr> <tr> <td><input type="checkbox" name="check_list[]" value="1000" onClick="valid(this);" /></td><td>BCA</td> <td><input type="checkbox" name="check_list[]" value="2000" onClick="valid(this);" /></td><td>MCA</td> </tr> <tr> <td><input type="checkbox" name="check_list[]" value="1200" onClick="valid(this);" /></td><td>BBA</td> <td><input type="checkbox" name="check_list[]" value="1000" onClick="valid(this);" /></td><td>MBA</td> </tr> <tr> <td colspan="2"><input type="text" name="quantity" id="quantity" placeholder="Quantity"></td> </tr> <tr> <td colspan="2"><input type="text" name="total" id="total" value="" placeholder="Total"></td> </tr> <tr> <td colspan="2"><input type="button" onClick="valid();" name="submit" value="Send" /></td> </tr> </table> </form>
Define var isFormSubimited = false;
定义var isFormSubimited = false;
Set isFormSubimited = true
in valid function. 在有效函数中设置isFormSubimited = true
。
Add " onchange='onQuantityChange()'
" to quantity input element. 在数量输入元素中添加“ onchange='onQuantityChange()'
“。
Add following function. 添加以下功能。
function onQuantityChange()
{
if(isFormSubimited && $("input[type='checkbox']:checked").length > 0)
{
//var _form = document.getElementById('frm');
//_form.submit();
valid();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.