2013-04-02 27 views
0

我是CakePHP的初學者,並試圖搜索這個問題,但沒有任何黃金。 我有一個選擇按鈕,當用戶選擇一個選項時,我必須觸發一個事件並將選定的值傳遞給控制器​​的操作。 代碼低於:cakephp獲取元素值js helper


<?php 
//I only need a select button why to bother of creating form 
echo $this->Form->input('district',array('label'=>'labelhere','id'=>'idhere')); 

//script handler for select option 
$this->Js->get('#idhere'); 
$this->Js->event(
     'change', 
     $this->Js->request(
      array('action' => 'listing',<the selected value of #idhere here>), 
      array('async' => true, 
       'method'=>'POST', 
       'update' => '#HFNames') 
     ) 
    ); 

?> 
<div id="HFNames"> 
Div to include ajax returns. 
</div> 

準確地說, 什麼是CakePHP的等效jQuery的$( '選擇')值()。

這裏是生成的HTML代碼:

<html> 
<div class="input select"><label for="idhere">labelhere</label><select name="data[district]" id="idhere"> 
<option value="01">Bhojpur</option> 
<option value="02">Dhankuta</option> 
<option value="03">Ilam</option> 
<option value="04">Jhapa</option> 
<option value="05">Khotang</option> 
<option value="06">Morang</option> 
<option value="07">Okhaldhunga</option> 
<option value="08">Panchthar</option> 
<option value="09">Sankhuwasabha</option> 
<option value="10">Saptari</option> 
<option value="11">Siraha</option> 
</select></div> 
</html> 

+0

給輸入的HTML源代碼。 – 2013-04-02 05:23:11

+0

什麼是html源碼? –

+1

其中html是由'echo $ this-> Form-> input('district',array('label'=>'labelhere','id'=>'idhere'))生成的;' – 2013-04-02 05:26:47

回答

2

試試這個

<?php 
//I only need a select button why to bother of creating form 
echo $this->Form->input('district',array('label'=>'labelhere','id'=>'idhere')); 

//script handler for select option 
$this->Js->get('#idhere'); 
$this->Js->event(
     'change', 
     $this->Js->request(
      array('action' => 'listing'), 
      array('async' => true, 
       'method'=>'POST', 
       'update' => '#HFNames', 
       'data' => '$("selector").value()') 
     ) 
    ); 

?> 
<div id="HFNames"> 
Div to include ajax returns. 
</div> 

騰出時間來閱讀這篇文章:Dynamic Web With Cake

+0

如何接受該數據在控制器? –

+0

'函數列表'中的var_dump($ this-> request-> data)'來查看數據是如何出現的。 – 2013-04-02 06:14:27