【问题标题】:cakephp passing selection to JS Helpercakephp 将选择传递给 JS Helper
【发布时间】:2013-03-07 07:03:08
【问题描述】:

我想获取选定的 schoolFilter 表单值并放入 [SELECTED VALUE HERE]。

$data = $this->Js->get('#SchoolFilter')->serializeForm(array('isForm' => true, 'inline' => true));

$this->Js->get('#SchoolSchoolId')->event(
'change', $this->Js->request(
        array('action' => 'assign_school_ads/', [SELECTED VALUE HERE], array(
            'update' => '#results',
            'data' => $data,
            'async' => true,
            'dataExpression' => true,
            'method' => 'POST'
        )
    )
);

// School Filter
$schoolFilter = $this->Form->create('School', array('id' => 'SchoolFilter');
$schoolFilter .= $this->Form->input('school_id', array('label'=>'Schools', 'empty'=>'- select -');
$schoolFilter .= $this->Form->end();

我看到了这个问题的变化,但没有任何明确的答案,只是忘记使用 JS Helper。在 JS Helper 的上下文中是否可能?如果没有,我可以使用常规 JQuery 获取值,然后将其注入 JS Helper。

【问题讨论】:

    标签: ajax cakephp helpers


    【解决方案1】:

    使用以下代码:-

    $this->Js->get('#SchoolFilter');
    $this->Js->event('change', $this->Js->request(array('controller'=>'put-ur-controller-ame-here','action' => 'assign_school_ads'),array('async' => true,'update' => '#results','method' => 'post','dataExpression'=>true,'data'=> $this->Js->serializeForm(array('isForm' => true,'inline' => true)))));
    

    serialize() 函数将表单数据发送到 php 操作,以便我们查看选择了哪个选项并决定在 ajax 调用中更新什么。

    表单数据将在操作中的$this->data 中找到(就像提交表单后一样)。

    不要忘记在布局中的正文结束标记之前添加$this->Js->writeBuffer();。否则所有的ajax代码都不会添加到你的页面中。

    【讨论】:

    • Shweta,我已经有了您建议的代码(请参阅我的代码),并且可以正常提交数据。我正在尝试获取选择的选定值并将其作为参数与操作一起发送:array('action' => 'assign_school_ads/', [SELECTED VALUE HERE]
    • 也许我不明白您将所选值作为参数发送的意思,因为可以在您的函数中以 $this->data['your-controller-name']['field-id 访问所选值'] 因为serializeForm 将向您的函数发送数据,就像在提交表单之后一样。
    • 例如,我正在尝试将 AJAX POST 操作重写为:assign_school_ads/3,其中 3 是选择菜单值。这不起作用:array('action' => 'assign_school_ads', $this->data['School']['school_id']。似乎 $data 在 AJAX 之后才能以这种格式提供给 Helper发生了。我知道我可以在控制器中获取 $data,但想知道是否有任何方法可以使用该操作来完成同样的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    相关资源
    最近更新 更多