简体   繁体   English

选择单选按钮时,jQuery验证不起作用

[英]jQuery validation not working when radio button selected

I have jQuery validation for my form. 我的表单有jQuery验证。 There is a radio box (attendance) and 2 drop down menus (colour and shade). 有一个单选框(出勤)和2个下拉菜单(颜色和阴影)。

It works perfectly if the user logs in for the very first time. 如果用户是第一次登录,它会完美地工作。 However because I have retreived the value from the database based on what they have selected before, if the user has already selected their choice and log back in, if they previously selected 'No' then my jQuery does not work. 但是,因为我已经基于他们之前选择的内容从数据库中获取了值,所以如果用户已经选择了他们的选择并重新登录,那么如果他们先前选择了“否”,那么我的jQuery将无法正常工作。 The two dropdown menus submit and are not greyed out as they should be. 这两个下拉菜单会提交,并且不会像应有的那样显示为灰色。 But if I click on 'No' radio box it does. 但是,如果我单击“否”单选框,它会。 Not sure if issue lies with jQuery or my radio button. 不知道问题出在jQuery还是我的单选按钮。

If the user logged in for the first time and selects 'No', they boxes grey out immediately. 如果用户首次登录并选择“否”,则他们的框会立即变灰。

jQuery validation: jQuery验证:

<script src="jquery.js"></script>
 <script>      
    $( function(){    
            function validate(id){       
                var enabled = ($("input[name='attendance" + id + "']:checked").val() == 'Yes');         
                if(enabled){              
                    //Please select option is selected              
                    if($("#colour" + id)[0].selectedIndex == 0){                 
                    alert('Please make your colourselection');                  
                    return false;             
                    }              
                    //Please select option is selected              
                    if($("#shade" + id)[0].selectedIndex == 0){                  
                        alert('Please select your shade');                  
                        return false;             
                    }   
                }     
                return true;    
            };

            $("input[name^='attendance']").click(function() {  

                var id = this.name.replace('attendance', '');      
                $("#colour" + id + ", #shade" + id).prop("disabled", this.value == 'No');         
                validate(id);    
            });      
           $("input:submit").click(function(){         
               var retVal = true;
               $.each([1], function(i, val){
                  retVal = (validate(val) && retVal);
               });
                return retVal;   
           }); 
         }); 
  </script>

radio button: 单选按钮:

<input name="attendance1" type="radio" id="Yes" value="Yes" checked="CHECKED" <?php if($row3['attendance1']=="Yes") { echo "checked"; }?>/>Attend with pleasure 
<br />
<input name="attendance1" type="radio" id="No" value="No" <?php if($row3['attendance1']=="No") { echo "checked"; }?>/>Decline with regret 

You are adding extra checked="checked" in Yes radio button, so it will always remain checked despite the value obtained from database, so try removing that, like, 您在是”单选按钮中添加了额外的check =“ =” checked“ ,因此尽管从数据库中获得了该值,它将始终保持选中状态,因此请尝试删除它,例如,


<input name="attendance1" type="radio" id="Yes" value="Yes" <?php if($row3['attendance1']=="Yes") { echo "checked"; }?>/>Attend with pleasure 
<br />
<input name="attendance1" type="radio" id="No" value="No" <?php if($row3['attendance1']=="No") { echo "checked"; }?>/>Decline with regret 

Hope it helps 希望能帮助到你

The problem I see is this part 我看到的问题是这部分

checked="CHECKED" <?php if($row3['attendance1']=="Yes") { echo "checked"; }?>

This will render to the following if $row3['attendance1'] is "Yes" 如果$row3['attendance1']为“是”,则将呈现以下内容

checked="CHECKED" checked

So this will create problem. 因此,这将产生问题。 Remove checked="CHEKCED" part. 删除checked="CHEKCED"部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM