我有一个简单的对话框小部件,我用它将一些值发送到服务器,并将一些东西添加到页面上显示的无序列表中。例如对于例如 name = matrixA a11 = 1 a12 = 2 a21 = 4 a22 = 1 所以这个数据被传递给服务器,并且name属性被附加到列表中。Jquery插入表单提交后丢失
用户按下提交后,会调用Validate()例程来验证条目,将名称条目追加到无序列表中并提交表单。
但是在表单提交后,页面被重新加载并且附件丢失。有什么办法可以避免这种情况?我知道这个追加工作可以在一秒钟内完成,但实际上这个页面会重新加载,我们又回到了开始阶段。
@EDIT:这是我的代码的一部分。整个代码变得太大,所以请查看 -
<script>
$('#form1').submit(function(event){
event.preventDefault();
//Validate is a function which returns a bool if validation proceeds correctly
var isCorrect = Validate(this);
if(isCorrect){
//if validated correctly then submit, close widget, add name of matrix to a list
this.submit();
$('#dialog').dialog('close'); //the form sits inside a dialog widget
$('#selectable').append("<li class='ui-widget-content'>name</li>");
}
});
</script>
<form name='form1' id='form1' method='get'>
<fieldset>
<label for="Name">Name</label>
<input type='text' name='nameofmatrix' id='Name' class='whitepanes'><br>
<label for="a11">a11</label>
<input type="text" name='a11' id='a11' class='whitepanes number-field'><br>
<label for="a22">a22</label>
<input type="text" name='a22' id='a22' class='whitepanes number-field'><br>
<label for="a12">a12</label>
<input type="text" name='a12' id='a12' class='whitepanes number-field'><br>
<label for="a21">a21</label>
<input type="text" name='a21' id='a21' class='whitepanes number-field'><br>
<input type='submit' name='mysubmit' id='submit_button' value='submit'>
<button id='cancel_button'>cancel</button>
</fieldset>
</form>
把一些代码看或更好发布[小提琴链接](http://jsfiddle.net/)。 – Jai
您可以使用ajax通过表单提交来防止页面重新加载。 – Vishal
http://stackoverflow.com/questions/7915827/combining-jquery-validation-followed-by-ajax-post/7916448#7916448 – mplungjan