2013-03-07 131 views
0

我想获得选定的schoolFilter表单值并放置在[SELECTED VALUE HERE]中。cakephp将选择传递给JS Helper

$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中。

回答

0

使用下面的代码: -

$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代码将不会被添加到您的页面。

+0

Shweta,我已经有你建议的代码(请参阅我的代码),它可以正常提交数据。我想获取选择的选定值并将其作为参数与动作一起发送:array('action'=>'assign_school_ads /',[SELECTED VALUE HERE] – drpudding 2013-03-14 17:02:41

+0

也许我没有明确指出发送所选内容值作为参数,因为选择的值可以在你的函数中作为$ this-> data ['your-controller-name'] ['field-id']作为serializeForm访问,就像在表单提交后发送数据到你的函数一样 – 2013-03-15 05:29:51

+0

例如,我试图将AJAX POST操作重写为:assign_school_ads/3,其中3是选择菜单值。这不起作用:array('action'=>'assign_school_ads',$ this-> data [ 'School'] ['school_id']看起来$ data在这种格式下不可用于Helper,直到AJAX发生之后,我知道我可以在控制器中获得$ data,但是想知道是否有任何方法可以使用行动来完成同样的事情。 – drpudding 2013-03-15 16:18:31