分两部分,第一部分是在页面上,根据数量的input标签内容,动态更新总价标签的内容,代码如下:
<script type="text/javascript"> ???$(document).ready(function () { ???????$("#iquantity").bind(‘input propertychange‘, function () { ???????????var quantity = $("#iquantity").val(); ???????????var price = 49; ???????????var total = price * quantity; ???????????$("#atotal").text(total); ???????????}); ???????});</script>
另一部分是按钮的验证,代码如下:
1 <script type="text/javascript"> 2 ????$(document).ready(function () { 3 ????????$("#btSave").click(function () { 4 ????????????if ($("#iquantity").val() == "") { 5 ????????????????alert("请填写购买数量\rPlease fill in the Quantity"); 6 ????????????????return false; 7 ????????????} 8 ????????????var zz = /^[1-9]\d*$/; 9 ????????????var qq = $("#iquantity").val();10 ????????????if (!zz.test(qq)) {11 ????????????????alert("请正确填写购买数量,格式为非零的正整数\rPlease fill in the correct format for the Quantity.A positive integer in the form of nonzero.");12 ????????????????return false;13 ????????????}14 ????????????$("#btSave").hide();15 ????????????var words = "请稍后...";16 ????????????$("#F").html(words);//F为一个span标签17 ????????????return true;18 ????????});19 ????});20 </script> ???
JS在页面根据数量改变总价及按钮进行格式验证
原文地址:https://www.cnblogs.com/Lvkang/p/9353012.html