我有一个表单,如果用户选择值 1,则提交表单,但如果他们选择 2 或 3,他们必须从带有类的选择中选择一个值reason select
(类有三个选择reason select
,我刚刚展示了一个这个问题使代码尽可能短)
包含在一个名为的 div 中的 htmlaudit_content
<tr>
<td>
<select name="grade" class="grade" >
<option value="1">1 - Perfect</option>
<option value="2">2 - Diagnostic</option>
<option value="3">3 - Unusable</option>
</select>
</td>
</tr>
<tr>
<td>
<select name="exposure_reason" class="reason_select" >
<option value="1">Reason One</option>
<option value="2">Reason Two</option>
<option value="3">Reason Three</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="button" class="delete_submit delete_from_audit" value=""
onClick="delete_image('<? echo $image_id; ?>','<? echo $audit_id; ?>')" />
<input type="button" class="reset_grade control_submit ie7_reset"
onclick="reset_image('<? echo $image_id; ?>','<? echo $audit_id; ?>')" value="" >
<input type="hidden" class="form_id" value="<? echo $i; ?>" >
<input type="button" name="audit_submit"
class="audit_submit audit_submit_btn ie7_submit" value="" />
</td>
</tr>
jQuery
$(document).ready(function(){
$('#audit_content').on("click", ".audit_submit", function(){
var form_id = $(this).prev('.form_id').val();
alert(form_id);
var $form = $(this);
reasonHasVal = false;
if ($form.find('select[name="grade"]').val() != "1") {
$form.find("select.reason_select").each(function() {
if ($(this).val() != "") {
reasonHasVal = true;
$.ajax({
type: "POST",
url: "ajax/image_audit.php",
dataType: "json",
data: $('#audit_form'+form_id).serialize(),
success: function(response){
$('.audited_ok_tick'+form_id).html(response);
},
error:function (xhr, ajaxOptions, thrownError){
alert(thrownError);
}
});
return false;
}
});
if (!reasonHasVal) {
e.preventDefault();
alert("Please select a reason.");
}
}
});
});
当我单击提交按钮时,警报正常,form_id
但没有别的,我知道 ajax 调用没问题,因为它一直有效,直到我尝试为选择值添加验证