代码之家  ›  专栏  ›  技术社区  ›  Glyn

jquery根据复选框的状态为变量赋值,以便提交

  •  0
  • Glyn  · 技术社区  · 7 年前

    我正在使用jQuery验证表单中的字段,然后根据是否选中复选框填充变量,这样我就可以将数据提交给Java程序。上面类似的逻辑工作;但是,它在“警报(5)”之后停止(即,未达到“警报(5a)”或“警报(5b)”)。如果复选框是问题的一部分(您可以选择“全部”(第一个复选框被选中,即在“警报(5)”之后)或其他8个复选框中的一个或多个复选框(两行)之后),我在之后添加了这些复选框的HTML。

    控制台中没有错误。

    我已经通过HTML代码进行了搜索,以确保类、ID和名称不会出现在其他地方。

    HTML:

    <div class="row">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <div class=" bg-warning col-lg-12 col-md-12 col-sm-12 col-xs-2" style="border:solid black;">
                <div class="form-group" style="display: block;">
                    <label class="control-label col-lg-2 col-md-2 col-sm-2 col-xs-2" for="attendance" style="line-height:50px">Attendance:<span class="req"> *</span></label>
                </div>
                <div class="controls col-lg-1 col-md-1 col-sm-1 col-xs-1">
                    <label class="checkbox-inline" for="attendanceAllCBs"><input type="checkbox" class="attendanceAll" value="All" id="attendanceAllCBs" name="attendanceAllCBs[]">All</label>
                </div>
    
                <div class=" col-lg-9 col-xs-9 col-sm-9">
                    <div id="top">
                        <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="Friday" id="attendanceOtherCBs" name="attendanceOtherCBs[]">Friday</label>
                        <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="Saturday" id="attendanceOtherCBs" name="attendanceOtherCBs[]">Saturday</label>
                        <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="Sunday" id="attendanceOtherCBs" name="attendanceOtherCBs[]">Sunday</label>
                        <label class="checkbox-inline" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="DaysOnly" id="attendanceOtherCBs" name="attendanceOtherCBs[]">Days Only</label>
                    </div>
                    <div id="bottom">
                        <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="FridayNight" id="attendanceOtherCBs" name="attendanceOtherCBs[]">Friday Night</label>
                        <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="SaturdayNight" id="attendanceOtherCBs" name="attendanceOtherCBs[]">Saturday Night</label>
                        <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="SundaydayNight" id="attendanceOtherCBs" name="attendanceOtherCBs[]">Sundayday Night</label>
                        <label class="checkbox-inline" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="Other" id="attendanceOtherCBs" name="attendanceOtherCBs[]">Other</label>
                    </div>
                </div>
            </div>
        </div>
    </div>
    

    jQuery:

             alert(4);
             var gender = '';
             if (genderCBs[0].checked === true){
                 gender = "M"
             }else{
                 gender = "F"
             }
             alert(5);
             var attendanceAllvar = '';
             if (attendanceAllCBs[0].checked === true){
                 alert("5a");
                 attendanceAllvar = "Y";
             }else{
                 alert("5b");
                 attendanceAllvar = "N";
             }
             alert(6);
             var attendanceFriday = '';
             if(attendanceOtherCBs[0].checked === true){
                 attendanceFriday = "Y";
             }else{
                 attendanceFriday = "N";
             }
    

    以下工作:

    HTML:

    <div class="controls col-lg-2 col-md-2 col-sm-2 col-xs-2">
        <span class="req"> *</span><label class="checkbox-inline" for="genderCBs"><input type="checkbox" class="gender" value="M" id="genderCBs" name="genderCBs[]">Male</label>
        <label class="checkbox-inline" for="genderCBs"><input type="checkbox" class="gender" value="F" id="genderCBs" name="genderCBs[]">Female</label>
    </div>
    

    jQuery:

    alert(4);
    var gender = '';
    if (genderCBs[0].checked === true){
        gender = "M"
    }else{
        gender = "F"
    }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Prakash Choudhary    7 年前

    试试这个:

    HTML:

    <div class="row">
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                <div class=" bg-warning col-lg-12 col-md-12 col-sm-12 col-xs-2" style="border:solid black;">
                    <div class="form-group" style="display: block;">
                        <label class="control-label col-lg-2 col-md-2 col-sm-2 col-xs-2" for="attendance" style="line-height:50px">Attendance:<span class="req"> *</span></label>
                    </div>
                    <div class="controls col-lg-1 col-md-1 col-sm-1 col-xs-1">
                        <label class="checkbox-inline" for="attendanceAllCBs"><input type="checkbox" class="attendanceAll" value="All" id="attendanceAllCBs" name="attendanceAllCBs[]">All</label>
                    </div>
    
                    <div class=" col-lg-9 col-xs-9 col-sm-9">
                        <div id="top">
                           <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="Friday" id="attendanceFriday" name="attendanceOtherCBs[]">Friday</label>
                            <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="Saturday" id="attendanceSaturday" name="attendanceOtherCBs[]">Saturday</label>
                            <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="Sunday" id="attendanceSunday" name="attendanceOtherCBs[]">Sunday</label>
                            <label class="checkbox-inline" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="DaysOnly" id="attendanceDaysOnly" name="attendanceOtherCBs[]">Days Only</label>
                        </div>
                        <div id="bottom">
                            <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="FridayNight" id="FridayNight" name="attendanceOtherCBs[]">Friday Night</label>
                            <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="SaturdayNight" id="SaturdayNight" name="attendanceOtherCBs[]">Saturday Night</label>
                            <label class="checkbox-inline col-lg-2" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="SundaydayNight" id="SundaydayNight" name="attendanceOtherCBs[]">Sundayday Night</label>
                            <label class="checkbox-inline" for="attendanceOtherCBs"><input type="checkbox" class="attendance2" value="Other" id="Other" name="attendanceOtherCBs[]">Other</label>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    
    
    
    
        jQuery:
    
                 alert(4);
                 var gender = '';
    
                 alert(5);
                 var attendanceAllvar = '';
                 if ($('#attendanceAllCBs').prop('checked') === true){
                     alert("5a");
                     attendanceAllvar = "Y";
                 }else{
                     alert("5b");
                     attendanceAllvar = "N";
                 }
                 alert(6);
                 var attendanceFriday = '';
                 if($('#attendanceFriday').prop('checked') === true){
                     attendanceFriday = "Y";
                 }else{
                     attendanceFriday = "N";
                 }
    
        2
  •  0
  •   Prakash Choudhary    7 年前

    试试这个:

      <div class="controls col-lg-2 col-md-2 col-sm-2 col-xs-2">
            <span class="req"> *</span><label class="checkbox-inline" for="genderCBs"><input type="checkbox" class="gender" value="M" id="genderMale" name="genderCBs[]">Male</label>
            <label class="checkbox-inline" for="genderCBs"><input type="checkbox" class="gender" value="F" id="genderFemale" name="genderCBs[]">Female</label>
        </div>
    
        alert(4);
        var gender = '';
        if ($('#genderMale').prop('checked') === true){
        alert(5);
            gender = "M"
        }else{
        alert(6);
            gender = "F"
        }