问题
I am working with cakePHP and in my view I have a form which has an ajax submit button. I rendered it using the cake helpers.
<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>
When I click submit, the controller action is called twice. I searched stackOverlow if this question existed but couldn't find a valid solution. There are no syntax errors.
Help will be really appreciated. Thanks.
回答1:
It sounds like the ajax event handlers aren't blocking the default behaviour. Try setting the form's onsubmit behaviour inline to prevent it (as cake's helper does):
<form onsubmit="event.returnValue = false; return false;" method="post" class="form-class" id="form-id" name="form1" style="display: none">
I recommend checking out the FormHelper for some useful shortcuts around this.
回答2:
This is the auto generated code:
$("#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();
})
来源:https://stackoverflow.com/questions/4824859/ajax-form-submit-form-submitted-twice