最好的jquery计算器实现和实现验证?

时间:2013-07-03 08:13:37

标签: jquery

嘿伙计这是我有史以来的第一个jquery脚本,我想知道你是否在那里看到任何错误或错误的habbits,我应该在我的代码或一般的任何东西中放松它...请注意我的恭维在某人的网站上验证它将是真实的东西。另外,您如何建议我推荐验证示例

<script type="text/javascript" src="jquery.js">
</script>
<script type="text/javascript">
$(document).ready(function(e) {
$(".calculatebutton").on("click",function()
{
//perform a input type check when i learn rregex
var item_cost=$("#itemCost").val();
if($(this).val()=="Cash Payment")

{
    var answer=item_cost*0.10;
}
else
{
    var answer=item_cost*0.12;
}
answer+=parseInt(item_cost);
$("#total").val(answer);
 })
});
</script>

1 个答案:

答案 0 :(得分:1)

带注释的小例子

http://jsfiddle.net/blackjim/JktYg/2/

$(document).ready(function (e) {
    $(".calculatebutton").on("click", function () {
        //perform a input type check when i learn rregex
        var answer = 0,
            item_cost = $("#itemCost").val(); // this is a string, NOT a number

        // check if you got a number, with your way or criteria
        if(isNaN(item_cost) || item_cost === true || item_cost === ''){  
            $("#total").val("Not a Number here");
            return false; // you didn't
        }
        item_cost = parseFloat(item_cost); // or use parseInt if you want integer

        if ($('.calculatebutton').text() === 'Cash Payment'){ // use always === for equality
            answer = item_cost * 0.10;
        } else {
            answer = item_cost * 0.12;
        }
        answer += item_cost;
        $("#total").val(answer);
    })
});