0

我正在使用 cakePHP,在我看来,我有一个带有 ajax 提交按钮的表单。我使用蛋糕助手渲染它。

    <form method="post" class="form-class" id="form-id" name="form1" style="display: none">
                *[content for the form]*
                    <?php
                    echo $ajax->submit('Ok',
                            array(
                                       'id'=> 'submit1',
                                        'url'=> array('controller'=>'c','action'=>'action1'),
                                        'complete'=> 'jsfunction()'

                                ));
                    echo $form->button('Submit',array('id'=>'cancel','value'=>'Cancel','onClick'=>'clickCancel()'));
                    ?>


</form>

当我单击提交时,控制器操作被调用两次。如果存在此问题,我搜索了 stackOverlow 但找不到有效的解决方案。没有语法错误。

帮助将不胜感激。谢谢。

4

2 回答 2

1

这是自动生成的代码:

 $("#submit1367508668").bind('click', function(){ 
     $.ajax({
              async:true, 
              type:'post', 
              complete:function(request, json) {
                   getServerResponse(request)
              }, 
              url:'/recipients/add', 
              dataType:'json', 
              data:$(this).parents('form:first').serialize()}); 
              blockUI(); 
     })
于 2011-01-30T12:44:42.993 回答
1

听起来 ajax 事件处理程序并没有阻止默认行为。尝试将表单的 onsubmit 行为设置为内联以防止它(就像 cake's helper 所做的那样):

<form onsubmit="event.returnValue = false; return false;" method="post" class="form-class" id="form-id" name="form1" style="display: none">

我建议查看FormHelper以了解一些有用的快捷方式。

于 2011-01-31T16:52:01.090 回答