弹出警报...有人能帮我吗

Popup Alert..... can someone help me

本文关键字:      更新时间:2024-05-09

我想要一个如图所示的输出http://farm8.staticflickr.com/7314/11269674244_6ddf5a65cf.jpg

问题是我如何连接按钮"same"中的文本"total"和文本"same1"

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>

我在jsfiddle中有一个演示。

此处演示

    (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);

它有帮助还是你想要?

相关文章:
  • 没有找到相关文章