2013-10-06 43 views
0

我有一个简单的对话框小部件,我用它将一些值发送到服务器,并将一些东西添加到页面上显示的无序列表中。例如对于例如 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> 
+0

把一些代码看或更好发布[小提琴链接](http://jsfiddle.net/)。 – Jai

+0

您可以使用ajax通过表单提交来防止页面重新加载。 – Vishal

+0

http://stackoverflow.com/questions/7915827/combining-jquery-validation-followed-by-ajax-post/7916448#7916448 – mplungjan

回答

0

您可能需要共享您的代码。

但我会做的是把点击监听器放在你的提交按钮上。然后使用防止默认来停止页面重新加载。

例如

$('#buttonID').click(function(event){ 
    event.preventDefault(); //important 
    // calculations here 
    // validate method 

}); 

或者为mplungjan提到的,你可以在你的form使用submit事件来代替。

$('#formID').submit(function(event){ 
    event.preventDefault(); //important 
    // calculations here 
    // validate method 

}); 
+1

使用表单提交事件在我看来更好! – mplungjan

+0

没错,谢谢你的参与。 – Trevor

+0

嘿Trevor,分享了一些代码(我应该在第一时间完成但是对于一些懒惰)。我已经在做一些类似于你发布的内容。问题在于,一旦在验证后提交,页面重新加载以及我所做的列表插入在验证后就消失了。 –