弹出警报.....有人可以帮助我

时间:2013-12-08 14:19:42

标签: javascript html

我希望在这张照片中有一个输出 http://farm8.staticflickr.com/7314/11269674244_6ddf5a65cf.jpg

问题是如何在按钮“same”和文本“same1”

中连接文本“total”

1:脚本

function myFunction(){

    var total = 0;
    $("input[type='checkbox'").each(function (index) {
        if ($(this).is(':checked')) {

            // This is checked checkbox !

            // Add the amount to total

            // Value is a string. So, you need to convert it integer (Numeric)
            total += parseInt($(this).attr("value"));
        }
    });
    // Show the final total value
    $("#total").val(total);
}

2:HTML

<form>

    <input name="100" type="checkbox" id="bike" value="100"><label for="bike">100</label><br>
    <input type="checkbox" id="bike2" value="100"><label for="bike2">100</label><br>
    <input type="checkbox" id="bike3" value="100"><label for="bike3">100</label><br>
    <input type="checkbox" id="bike4" value="100"><label for="bike4">100</label><br>
    <input type="checkbox" id="bike5" value="100"><label for="bike5">100</label><br>
    <input type="checkbox" id="bike6" value="100"><label for="bike6">100</label><br>
    <input type="checkbox" id="bike7" value="100"><label for="bike7">100</label><br>
    <input type="checkbox" id="bike8" value="100"><label for="bike8">100</label><br>
    <input type="checkbox" id="bike9" value="100"><label for="bike9">100</label><br>
    <input type="checkbox" id="bike1" value="100"><label for="bike1">100</label><br>
    <button type="button" onclick="myFunction()">Equal</button>
    <input onclick="myFunction()" type="text" id="total" value="0"><br>
    <button type="button" onclick="">Same</button>
    <input onclick="" type="text" id="same1" value="0">
</form>

1 个答案:

答案 0 :(得分:0)

我在jsfiddle有一个演示。

Demo Here

    (function($){
    $(document).ready(function(){
        $('form').on('click',function(e){
            switch(e.target.id){
                case 'btnEqual':
                    setTotal(getTotal());
                    break;
                case 'btnSame':
                    showMsg();
                    break;
                default:break;
            }
        });
    });

    function getTotal(){

        var total = 0;
        $("input[type='checkbox'").each(function (index) {
            if ($(this).is(':checked')) {
                // This is checked checkbox !
                // Add the amount to total
                // Value is a string. So, you need to convert it integer (Numeric)
                total += parseInt($(this).attr("value"),10);
            }
        });

        return total;
    };

    function setTotal(total){
        $("#total").val(total);
    };

    function getSame(){
        return parseInt($('#same1').val(),10);        
    };

    function showMsg(){

        var sameVal=getSame(),selectedTotal=getTotal();

        if(sameVal>=selectedTotal){

            console.log('Good');

        }else{

            console.log('Wrong');

        }
    };

})(jQuery);

这有用还是你想要的?