javascript变量不会在函数内部循环递增

时间:2015-12-29 11:39:12

标签: javascript jquery loops

此脚本提取标签并执行一个函数示例,如果02之间的年龄在此示例中得到它-s id 48,则在执行后递增g0一个函数getResponse _来宾类型,每个类型为id,而且年龄大小分别为g0g1

我无法在循环中获得g0增加;它给出的日期我不知道它的来源,对不起我的想法,感谢帮助:

arr = [];
arr2 = [];
arr3 = [];

var g0 = 5, g1 = 0, g2 = 0, g3 = 0, g4 = 0, g5 = 0, g6 = 0, g7 = 0;

jomresJquery("label[for^=guesttype]").each(function(index){

    if(index!=0) {

        arr[index] = jomresJquery(this).attr("for").replace ( /[^\d.]/g, '' );
        arr2[index] = jomresJquery(this).text().split('-')[0];
        arr3[index] = jomresJquery(this).text().split('-')[1];

    }

});

jomresJquery("select[id^=guesttype]").each(function(index, g0, g1, g2, g3, g4, g5, g6){

    if(index==1) {

        var self = jomresJquery(this);
        var quantity = self.val();
        var html = "";

        for (var i = 0; i < quantity; i++) {

            var k=0;
            html += '<li><div class="form-group"><label>Child '+ (i + 1) +'</label><div class="input-group"><input required type="text" class="input-small form-control input-group" name="childrenkjhgfd_ages'+ i +'" id="childrenkjhgfd_ages'+ i +'"><script>jomresJquery(function(g0,g1,g2,g3,g4,g5,g6) {jomresJquery("#childrenkjhgfd_ages'+ i +'").datepicker({yearRange: "-15:+0" ,changeYear: true,changeMonth: true,onSelect: function(g0,g1,g2,g3,g4,g5,g6){var birthDay = jomresJquery("#childrenkjhgfd_ages'+i+'").val();var DOB = new Date(birthDay);var today = new Date();var age = today.getTime() - DOB.getTime();age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25));for (var k = 0; k <= arr.length; k++) {if ((age >= arr2[k])&(age <= arr3[k])) {g'+k+'+=1;alert(g'+k+'+" , "+arr[k])}}}});})</script><img class="ui-datepicker-trigger" src="" alt="" title=""><span id="dp_trigger_childrenkjhgfd_ages'+ i +'" class="input-group-addon"><span class="fa fa-calendar"></span></span></div></div></li>';

        }

        self.next(".child-age-details").find("ul").empty().append(html);

    //});
    }

    if(index!=0) jomresJquery(this).hide();

});

这是html代码:

<div class="col-md-3">
    <div class="form-group">

        <label for="guesttype44" class="control-label">Adulte</label>

        <select class="input-small" onchange="getResponse_guesttype('44',this.value);" size="1" id="guesttype44" name="guesttype44">
            <option value="0">00</option>
            <option value="1">01</option>
            <option selected="selected" value="2">02</option>
            <option value="3">03</option>
            <option value="4">04</option>
            <option value="5">05</option>
            <option value="6">06</option>
            <option value="7">07</option>
            <option value="8">08</option>
            <option value="9">09</option>
            <option value="10">10</option>
        </select>

    </div>      
</div>

<div class="col-md-3">

    <div class="form-group">

        <label for="guesttype48" class="control-label" style="display: none;">0-2</label>

        <select class="input-small" onchange="getResponse_guesttype('48',this.value);" size="1" id="guesttype48" name="guesttype48" style="display: none;">
            <option selected="selected" value="0">00</option>
            <option value="1">01</option>
            <option value="2">02</option>
            <option value="3">03</option>
            <option value="4">04</option>
            <option value="5">05</option>
            <option value="6">06</option>
            <option value="7">07</option>
            <option value="8">08</option>
            <option value="9">09</option>
            <option value="10">10</option>

        </select>

        <label>CHILD</label>

        <select name="jform[room_types][21][163][0][children_number]" class="input-small reservation-form-child-quantity trigger_tariff_calculating occupancy_child_21_163_0 valid">

            <option value="">0</option>
            <option value="1">1 child</option>
            <option value="2">2 children</option>
            <option value="3">3 children</option>

        </select>

        <div class="span12 child-age-details">

            <p>Age of child(ren) at checkout</p>

            <ul class="unstyled">
                <li>
                    <div class="form-group">
                        <label>Child 1</label>
                        <div class="input-group">
                            <input type="text" id="childrenkjhgfd_ages0" name="childrenkjhgfd_ages0" class="input-small form-control input-group hasDatepicker" required="">
                            <script>
                                jomresJquery(function(g0, g1, g2, g3, g4, g5, g6) {
                                    jomresJquery("#childrenkjhgfd_ages0").datepicker({yearRange: "-15:+0" , changeYear: true, changeMonth: true, onSelect: function(g0, g1, g2, g3, g4, g5, g6){
                                        var birthDay = jomresJquery("#childrenkjhgfd_ages0").val();
                                        var DOB = new Date(birthDay);
                                        var today = new Date();
                                        var age = today.getTime() - DOB.getTime();
                                        age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25));
                                        for (var k = 0; k &lt;= arr.length; k++) {
                                            if ((age &gt;= arr2[k])&amp;(age &lt;= arr3[k])) {
                                                g0 += 1;
                                                alert(g0 + " , " + arr[k])
                                            }
                                        }
                                    }});
                                })
                            </script>

                            <img title="" alt="" src="" class="ui-datepicker-trigger">
                            <span class="input-group-addon" id="dp_trigger_childrenkjhgfd_ages0">
                                <span class="fa fa-calendar"></span>
                            </span>
                        </div>
                    </div>
                </li>

                <li>
                    <div class="form-group">
                        <label>Child 2</label>
                        <div class="input-group">
                            <input type="text" id="childrenkjhgfd_ages1" name="childrenkjhgfd_ages1" class="input-small form-control input-group hasDatepicker" required="">
                            <script>
                                jomresJquery(function(g0, g1, g2, g3, g4, g5, g6) {
                                    jomresJquery("#childrenkjhgfd_ages1").datepicker({yearRange: "-15:+0", changeYear: true, changeMonth: true, onSelect: function(g0, g1, g2, g3, g4, g5, g6){
                                        var birthDay = jomresJquery("#childrenkjhgfd_ages1").val();
                                        var DOB = new Date(birthDay);
                                        var today = new Date();
                                        var age = today.getTime() - DOB.getTime();
                                        age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25));
                                        for (var k = 0; k &lt;= arr.length; k++) {
                                            if ((age &gt;= arr2[k])&amp;(age &lt;= arr3[k])) {
                                                g0 += 1;
                                                alert(g0 + " , " + arr[k])
                                            }
                                        }
                                    }});
                                })
                            </script>

                            <img title="" alt="" src="" class="ui-datepicker-trigger">
                            <span class="input-group-addon" id="dp_trigger_childrenkjhgfd_ages1">
                                <span class="fa fa-calendar"></span>
                            </span>
                        </div>
                    </div>
                </li>

                <li>
                    <div class="form-group">
                        <label>Child 3</label>
                        <div class="input-group">
                            <input type="text" id="childrenkjhgfd_ages2" name="childrenkjhgfd_ages2" class="input-small form-control input-group hasDatepicker" required="">
                            <script>
                                jomresJquery(function(g0, g1, g2, g3, g4, g5, g6) {
                                    jomresJquery("#childrenkjhgfd_ages2").datepicker({yearRange: "-15:+0", changeYear: true, changeMonth: true, onSelect: function(g0, g1, g2, g3, g4, g5, g6){
                                        var birthDay = jomresJquery("#childrenkjhgfd_ages2").val();
                                        var DOB = new Date(birthDay);
                                        var today = new Date();
                                        var age = today.getTime() - DOB.getTime();
                                        age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25));
                                        for (var k = 0; k &lt;= arr.length; k++) {
                                            if ((age &gt;= arr2[k])&amp;(age &lt;= arr3[k])) {
                                                g0 += 1;
                                                alert(g0 + " , " + arr[k])
                                            }
                                        }
                                    }});
                                })
                            </script>

                            <img title="" alt="" src="" class="ui-datepicker-trigger">
                            <span class="input-group-addon" id="dp_trigger_childrenkjhgfd_ages2">
                                <span class="fa fa-calendar"></span>
                            </span>
                        </div>
                    </div>
                </li>                
            </ul>
        </div>
    </div>
</div>

<div class="col-md-3">
    <div class="form-group">
        <label for="guesttype48" class="control-label" style="display: none;">0-2</label>

            <select class="input-small" onchange="getResponse_guesttype('48',this.value);" size="1" id="guesttype48" name="guesttype48" style="display: none;">

                <option selected="selected" value="0">00</option>
                <option value="1">01</option>
                <option value="2">02</option>
                <option value="3">03</option>
                <option value="4">04</option>
                <option value="5">05</option>
                <option value="6">06</option>
                <option value="7">07</option>
                <option value="8">08</option>
                <option value="9">09</option>
                <option value="10">10</option>

            </select>

            <label>CHILD</label>

            <select name="jform[room_types][21][163][0][children_number]" class="input-small reservation-form-child-quantity trigger_tariff_calculating occupancy_child_21_163_0 valid">

                <option value="">0</option>
                <option value="1">1 child</option>
                <option value="2">2 children</option>
                <option value="3">3 children</option>

            </select>

            <div class="span12 child-age-details">
                <p>Age of child(ren) at checkout</p>
                    <ul class="unstyled">
                        <li>
                            <div class="form-group">
                                <label>Child 1</label>
                                <div class="input-group">
                                    <input type="text" id="childrenkjhgfd_ages0" name="childrenkjhgfd_ages0" class="input-small form-control input-group hasDatepicker" required="">
                                <script>
                                    jomresJquery(function(g0, g1, g2, g3, g4, g5, g6) {
                                        jomresJquery("#childrenkjhgfd_ages0").datepicker({yearRange: "-15:+0", changeYear: true, changeMonth: true, onSelect: function(g0, g1, g2, g3, g4, g5, g6){
                                            var birthDay = jomresJquery("#childrenkjhgfd_ages0").val();
                                            var DOB = new Date(birthDay);
                                            var today = new Date();
                                            var age = today.getTime() - DOB.getTime();
                                            age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25));
                                            for (var k = 0; k &lt;= arr.length; k++) {
                                                if ((age &gt;= arr2[k])&amp;(age &lt;= arr3[k])) {
                                                    g0 + =1;
                                                    alert(g0 + " , " + arr[k])
                                                }
                                            }
                                        }});
                                    })
                                </script>

                                <img title="" alt="" src="" class="ui-datepicker-trigger">
                                <span class="input-group-addon" id="dp_trigger_childrenkjhgfd_ages0">
                                    <span class="fa fa-calendar"></span>
                                </span>
                            </div>
                        </div>
                    </li>

                    <li>
                        <div class="form-group">
                            <label>Child 2</label>
                            <div class="input-group">
                                <input type="text" id="childrenkjhgfd_ages1" name="childrenkjhgfd_ages1" class="input-small form-control input-group hasDatepicker" required="">
                                <script>
                                    jomresJquery(function(g0, g1, g2, g3, g4, g5, g6) {
                                        jomresJquery("#childrenkjhgfd_ages1").datepicker({yearRange: "-15:+0", changeYear: true, changeMonth: true, onSelect: function(g0, g1, g2, g3, g4, g5, g6){
                                            var birthDay = jomresJquery("#childrenkjhgfd_ages1").val();
                                            var DOB = new Date(birthDay);
                                            var today = new Date();
                                            var age = today.getTime() - DOB.getTime();
                                            age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25));
                                            for (var k = 0; k &lt;= arr.length; k++) {
                                                if ((age &gt;= arr2[k])&amp;(age &lt;= arr3[k])) {
                                                    g0 += 1;
                                                    alert(g0 + " , " + arr[k])
                                                }
                                            }
                                        }});
                                    })
                                </script>

                                <img title="" alt="" src="" class="ui-datepicker-trigger">
                                <span class="input-group-addon" id="dp_trigger_childrenkjhgfd_ages1">
                                    <span class="fa fa-calendar"></span>
                                </span>
                            </div>
                        </div>
                    </li>

                    <li>
                        <div class="form-group">
                            <label>Child 3</label>
                            <div class="input-group">
                                <input type="text" id="childrenkjhgfd_ages2" name="childrenkjhgfd_ages2" class="input-small form-control input-group hasDatepicker" required="">
                                <script>
                                    jomresJquery(function(g0, g1, g2, g3, g4, g5, g6) {
                                    jomresJquery("#childrenkjhgfd_ages2").datepicker({yearRange: "-15:+0" , changeYear: true, changeMonth: true, onSelect: function(g0, g1, g2, g3, g4, g5, g6){
                                        var birthDay = jomresJquery("#childrenkjhgfd_ages2").val();
                                        var DOB = new Date(birthDay);
                                        var today = new Date();
                                        var age = today.getTime() - DOB.getTime();
                                        age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25));
                                        for (var k = 0; k &lt;= arr.length; k++) {
                                            if ((age &gt;= arr2[k])&amp;(age &lt;= arr3[k])) {
                                                g0 += 1;
                                                alert(g0 + " , " + arr[k])
                                            }
                                        }
                                    }});
                                })
                            </script>

                            <img title="" alt="" src="" class="ui-datepicker-trigger">
                            <span class="input-group-addon" id="dp_trigger_childrenkjhgfd_ages2">
                                <span class="fa fa-calendar"></span>
                            </span>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</div>

<div class="col-md-3">
    <div class="form-group">
        <label for="guesttype49" class="control-label" style="display: none;">2-5</label>

            <select class="input-small" onchange="getResponse_guesttype('49',this.value);" size="1" id="guesttype49" name="guesttype49" style="display: none;">
                <option selected="selected" value="0">00</option>
                <option value="1">01</option>
                <option value="2">02</option>
                <option value="3">03</option>
                <option value="4">04</option>
                <option value="5">05</option>
                <option value="6">06</option>
                <option value="7">07</option>
                <option value="8">08</option>
                <option value="9">09</option>
                <option value="10">10</option>

            </select>

        </div>
    </div>

        <div class="col-md-3">
            <div class="form-group">
                <label for="guesttype50" class="control-label" style="display: none;">5-10</label>
                    <select class="input-small" onchange="getResponse_guesttype('50',this.value);" size="1" id="guesttype50" name="guesttype50" style="display: none;">

                        <option selected="selected" value="0">00</option>
                        <option value="1">01</option>
                        <option value="2">02</option>
                        <option value="3">03</option>
                        <option value="4">04</option>
                        <option value="5">05</option>
                        <option value="6">06</option>
                        <option value="7">07</option>
                        <option value="8">08</option>
                        <option value="9">09</option>
                        <option value="10">10</option>

                    </select>
                </div>
            </div>                                                  

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,我把增量放在一个隐藏的输入框中