我對錶單有jQuery驗證。有一個收音機(出席)和2個下拉菜單(顏色和陰影)。單選按鈕時jQuery驗證不起作用
如果用戶第一次登錄,它可以很好地工作。但是,因爲我已經從數據庫中檢索了他們之前選擇的值,如果用戶已經選擇了他們的選擇並重新登錄,如果他們之前選擇了「否」,那麼我的jQuery不起作用。這兩個下拉菜單提交併且不會變灰,因爲它們應該是。但是,如果我點擊「否」收音機盒就可以了。不知道問題出在jQuery或我的單選按鈕上。
如果用戶第一次登錄並選擇「否」,它們會立即變灰。
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>
單選按鈕:
<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