<script type="text/javascript">
$(document).ready(function(){
$('#addprojectform').live('submit', function (e) {
e.preventDefault();
$(".error").remove();
var startdate = $("#startdate").val();
var client = $('#client').val();
var source_lang= $('#source_lang option:selected').length;
var targ_lang = $('#targ_lang option:selected').length;
var qty = $('#qty').val();
var hasError = false;
if(startdate == '') {
$("#startdate").after('<span class="error" style="color: red;">This field is required.</span>');
hasError = true;
}
if(client == '') {
$("#client").after('<span class="error" style="color: red;">This field is required. </span>');
hasError = true;
}
if(!(source_lang)) {
$("#source_lang").after('<span class="error" style="color: red;">This field is required.</span>');
hasError = true;
}
if(!(targ_lang)) {
$("#targ_lang").after('<span class="error" style="color: red;">This field is required.</span>');
hasError = true;
}
if(qty == '') {
$("#qty").after('<span class="error" style="color: red;">This field is required.</span>');
hasError = true;
}
} // if translation
if(hasError == false)
return true;
});
});
</script>
我有這段代碼。我想在提交之前驗證表單。我無法控制表單沒有錯誤。如果沒有發現錯誤,我想提交表單。我添加了下面的代碼:使用jQuery進行驗證後無法提交表單(使用preventDefault())
if(hasError == false)
return true;
但是沒有提交表格..任何人都可以幫忙嗎?
謝謝! :) Fabrizio Calderan – rakela