我有一个提交表单,我检查是否选择了某种类型的选项我的验证会根据它进行更改,但它无法以某种方式工作,任何机构都可以帮助我这是我的 HTML 表单:
<form method="post" id="addnews" action="/app/index.php/admin/sizes/save" enctype="multipart/form-data" style="margin-top:0px" name="addnews">
<table width="100%" class="font">
<tbody><tr>
<td colspan="2"><span style="color: rgb(204, 0, 0); padding-left: 302px;">( Note:Fields marked with * are required )</span></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td style="width:301px;text-align:right; vertical-align:top;"><label><font size="4" color="Red">*</font> Size Type: </label></td>
<td style="text-align:left">
<select onchange="getForm(this.value);" style="width:206px;" name="size_type" id="size_type" class="common_form">
<option value="0">Template</option><option value="1"> Wallart</option> </select>
</td>
</tr>
<tr>
<td colspan="2">
<table width="100%" cellspacing="3" cellpadding="0" style="display:block inline;" id="templateForm" class="font">
<tbody><tr>
<td style="text-align:right; vertical-align:middle;">
<label><font size="4" color="Red">*</font> Name: </label>
</td>
<td style="text-align:left">
<input type="text" value="" maxlength="200" id="name" name="name" class="common_form">
<label style="display:none;" id="name_lbl">Please enter name</label>
</td>
</tr>
<tr>
<td style="text-align:right; vertical-align:top;"><label><font size="4" color="Red">*</font> Code: </label></td>
<td style="text-align:left">
<input type="text" value="" maxlength="200" id="code" name="code" class="common_form">
<label style="display:none;" id="code_lbl">Please enter code</label>
</td>
</tr>
<tr>
<td style="text-align:right; vertical-align:top;"><label><font size="4" color="Red">*</font> Num Code: </label></td>
<td style="text-align:left">
<input type="text" value="" maxlength="200" id="num_code" name="num_code" class="common_form">
<label style="display:none;" id="numc_lbl">Please enter num code</label>
</td>
</tr>
</tbody></table>
<table width="100%" cellspacing="3" cellpadding="0" style="display:none;" id="wallartForm" class="font">
<tbody><tr>
<td style="text-align:right; vertical-align:middle;width:299px;">
<label><font size="4" color="Red">*</font> Width: </label>
</td>
<td style="text-align:left">
<input type="text" value="" maxlength="200" id="width" name="width" class="common_form">
</td>
</tr>
<tr>
<td style="text-align:right; vertical-align:top;"><label><font size="4" color="Red">*</font> Height: </label></td>
<td style="text-align:left">
<input type="text" value="" maxlength="200" id="height" name="height" class="common_form">
</td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td style="text-align:right"><label>Status: </label></td>
<td style="text-align:left">
<input type="radio" checked="checked" value="1" name="is_active">Active
<input type="radio" value="0" name="is_active">Inactive
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td> </td>
<td style="text-align:left">
<input type="hidden" value="" name="id" id="id">
<input type="submit" value="Save" id="submitT" onclick="if($('#size_type').val() == 1){check('W',this.form);}else{check('T',this.form);}" class="button115_a">
<input type="button" onclick="back();" value="Back" class="button90_b">
</td>
</tr>
</tbody></table>
</form>
在选择框更改时,如果之前完成了任何验证,我会隐藏表单的某些字段也会重置我的表单:
function getForm(id)
{
var validator = $("#addnews").validate();
validator.resetForm();
if(id == 1)
{
$("#templateForm").attr("style","display:none;");
$("#wallartForm").attr("style","display:block inline;");
}
else
{
$("#templateForm").attr("style","display:block inline;");
$("#wallartForm").attr("style","display:none;");
}
}
这是我的验证功能:
function check(type){
if(type == "T"){
$('#addnews').validate({
rules: {
name:{required:true},
code:{required:true},
num_code:{required:true}
},
messages:{
name:{required:"<br>Please enter size name"},
code:{required:"<br>Please enter size code"},
num_code:{required:"<br>Please enter size num code"}
}
});
}
else{
alert("yes")
$('#addnews').validate({
rules: {
width:{required:true},
height:{required:true}
},
messages:{
width:{required:"<br>Please enter width"},
height:{required:"<br>Please enter height"}
}
});
}
}
验证适用于模板选择,但不适用于 wallart 选择