使用for循环函数中的JQuery Change Event

时间:2016-04-21 03:19:30

标签: javascript jquery forms select

<script type="text/javascript">
function total(){
var count = jQuery("[id^=frm_section_430]").length;
var Total = 0;
    for(var i=0; i < count; i++){
        var date = jQuery("#field_h1tvhv").val();
                var splitdate = date.split('/');
        var start = jQuery("#field_qgd9fa-" + i).val();   //eg "09:20 PM"
        var end = jQuery("#field_701ehm-" + i).val();   //eg "10:00 PM"

                //start time conversion to 24hr
        var Starthours = Number(start.match(/^(\d+)/)[1]);
        var Startminutes = Number(start.match(/:(\d+)/)[1]);
        var StartAMPM = start.match(/\s(.*)$/)[1];

        if (Startminutes == 15){
                      Startminutes = 25;
                }else if(Startminutes == 30){
                      Startminutes = 50;
                }else if(Startminutes == 45){
                      Startminutes = 75;
                }

        if (StartAMPM == "PM" && Starthours < 12) Starthours = Starthours + 12;
        if (StartAMPM == "AM" && Starthours == 12) Starthours = Starthours - 12;

        var sHoursStart = Starthours.toString();
        var sMinutesStart = Startminutes.toString();

        if (Starthours < 10) sHoursStart = sHoursStart ;
        if (Startminutes > 10) sMinutesStart = sMinutesStart;

                //End time conversion to 24hr
        var Endhours = Number(end.match(/^(\d+)/)[1]);
        var Endminutes = Number(end.match(/:(\d+)/)[1]);
        var EndAMPM = end.match(/\s(.*)$/)[1];

        if (Endminutes == 15){
                      Endminutes = 25;
                }else if(Endminutes == 30){
                      Endminutes = 50;
                }else if(Endminutes == 45){
                      Endminutes = 75;
                }

        if (EndAMPM == "PM" && Endhours < 12) Endhours = Endhours + 12;
        if (EndAMPM == "AM" && Endhours == 12) Endhours = Endhours - 12;

        var sHoursEnd = Endhours.toString();
        var sMinutesEnd = Endminutes.toString();

        if (Endhours < 10) sHoursEnd = sHoursEnd;
        if (Endminutes > 10) sMinutesEnd = sMinutesEnd;

                var hrStart = sHoursStart + "." + sMinutesStart;
                var hrEnd = sHoursEnd + "." + sMinutesEnd;

                if((hrEnd - hrStart) >= 0){
                    Total += hrEnd - hrStart;
                    jQuery("#field_deb1y1").val(Total.toFixed(2));
                }else{
                    alert("Start value cannot be less than end value. Please try again!");
                    jQuery("#field_qgd9fa-" + i).val("06:00 AM");
                    jQuery("#field_701ehm-" + i).val("06:00 AM");
                }
    }
       return Total;
}

jQuery(document).ready(function() {
    jQuery("#field_qgd9fa-0").val("06:00 AM");
    jQuery("#field_701ehm-0").val("06:00 AM");

//Script in original questioning
//jQuery("[id^=field_qgd9fa-], [id^=field_701ehm-]").change(function () {
    //total();
//});

jQuery(document).on("change",  "[id^=field_qgd9fa-], [id^=field_701ehm-]", function () {
    total();
});

jQuery("#frm_checkbox_429-").hide();
jQuery("#field_fvb50h-").hide();

});
</script>

更改事件仅发生在第一个选择框中。我确实注意到当我更改下一个选择框时该功能总计,但只有当我回去玩第一个选择框时才会这样。我做错了什么?

0 个答案:

没有答案