问问题
1308 次
3 回答
2
$('#formId').submit(function (e) {
if ($('#assettype').val() == '') {
e.preventDefault(); //stop form submission
}
});
或者
$.validator.addMethod('notNone', function (value, element) {
return (value != '');
}, 'Please select an option');
$("form").validate({
rules: {
assettype: {
required: true,
notNone: true
}
}
});
于 2013-11-12T15:48:55.590 回答
1
这似乎您在提交之前需要一个简单的验证
$('#formId').submit(function () {
if ($('#assettype').val() != '')) { //Check the value of select
return true; //allow to submit the form
}
else {
alert("Please select an option");
return false; //Prevent form being submitted
}
});
于 2013-11-12T15:48:29.587 回答
1
<form id="my_form" method="POST" action="" >
<select id="assettype" name="assettype" class="input-medium required">
<option value="0" >-- Please select --</option>
<option value="1">Printer</option>
<option value="2">Scanner</option>
</select>
</form>
<script type="text/javascript">
$("#my_form").submit(
var check_value = $("#assettype").val();
if (check_value == 0)
{
e.preventDefault();
// prevent the user he has to choose something
// by an alert for example, or background change of the element
$("#assettype").css("background-color", "red");
return false;
}
else
{
// and restore it if it's ok
$("#assettype").css("background-color", "inherit");
}
return true;
});
</script>
于 2013-11-12T15:59:16.700 回答