我有这样的代码
<form onsubmit="return false;" id="myform">
<input type="text" id="data"/>
<input type="submit" value="Submit" id="submitForm" />
</form>
<script>
$(document).ready( function() {
var form = $('#myform');
var result = form.find('#data').val() != "";
if ($.data( form, 'validated' ) && result) {
return true;
}
else
$.data( form, 'validated', false );
$.post( 'http://myweb/action', form.serialize(), function (data) {
var json_result = $.parseJSON(data);
if (result && json_result.is_valid) {
$.data( form, 'validated', true );
form.attr('onsubmit', 'return true;' );
form.submit();
}
else {
alert( 'There is an error in the form data!' );
}
}
});
</script>
是一个好习惯吗?