我在 cakephp 2 中构建 ajax 表单时遇到问题,自 1.3 以来,它显然已经发生了很大变化。
我使用以下代码:
<div id="commentForm">
<div id="commentStatus"></div>
<?php
echo $this->Form->create('Comment', array('action' => 'save', 'default' => false));
echo $this->Form->input('Comment.comments_name');
echo $this->Form->input('Comment.comments_email');
echo $this->Form->input('Comment.comments_text');
echo $this->Js->submit('Save', array('update' => '#commentStatus'));
echo $this->Form->end();
?>
但是,按下按钮时不会提交表单。
我将非常感谢您的帮助!
谢谢!
最佳答案
在您的 View 文件中尝试此操作:
<?php
$data = $this->Js->get('#CommentSaveForm')->serializeForm(array('isForm' => true, 'inline' => true));
$this->Js->get('#CommentSaveForm')->event(
'submit',
$this->Js->request(
array('action' => 'save'),
array(
'update' => '#commentStatus',
'data' => $data,
'async' => true,
'dataExpression'=>true,
'method' => 'POST'
)
)
);
echo $this->Form->create('Comment', array('action' => 'save', 'default' => false));
echo $this->Form->input('Comment.comments_name');
echo $this->Form->input('Comment.comments_email');
echo $this->Form->input('Comment.comments_text');
echo $this->Form->end(__('Submit'));
echo $this->Js->writeBuffer();
?>
注意: #CommentSaveForm
是由 CakePHP 生成的 ID,如果您有自己的 ID,请使用它
关于jquery - cakephp 2 ajax 形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10198137/