2013-03-28 67 views
0

我已經有一個表單填滿了複選框,我正在嘗試通過AJAX更新表單選項(因爲選擇了其中一個選項是有限的)。我通過提交按鈕來完成這項工作,但我希望在選擇框中完成onchange。 (是的,我知道這是提交表單的一種不尋常的用法)。將Onchange事件添加到FormHelper表單

我嘗試將onchange添加到輸入的屬性數組中,但它不起作用。其他的東西,如標籤,類等都可以很好地應用,但onchange不會。這裏的複選框列表中的一個:

echo $this->Form->input('Sale.LocationID', array(
     'label' => 'LocationID:', 
     'options'=>$locations, 
     'multiple' => 'checkbox', 
     'onchange'=>"this.form.submit()", 
     )); 

有什麼辦法給onchange事件在CakePHP的標準方式添加到我的複選框,否則我將不得不建立一個沒有表單助手複選框來做到這一點?

+0

你確定它不是?我只是在我的應用程序中試過你的代碼,並且它添加了onchange代碼就好了。 (我在2.3.1中,但我不認爲這會影響任何東西) – Dave

+0

@Dave在HTML中做了哪些onchange最終?我沒有看到從父div到輸入標籤的任何地方,並沒有看到它 –

回答

1

使用內聯事件處理效率不是很高,您是否考慮過使用jQuery和事件委託?

在視圖文件的末尾;

// Using 'heredoc' here so we don't have to escape quotes 
$script = <<< JS 

    // this will handle click events on any checkbox on the page 
    $(document).on("click", "input[type='checkbox']", function(){ 
     // or AJAX post here 
     this.form.submit(); 
    }); 
JS; // note: must be at the start of the line, no space before JS 

// Append the script to the buffer 
$this->Js->buffer($script); 

而就在該佈局內(例如default.thtml中)

// output the JavaScript buffer 
echo $this->Js->writeBuffer(); 
+0

* *實際上是一個更好的主意,不知道爲什麼我沒有想到它......我已經這樣做了也以其他形式。 –

+0

很高興我可以幫助:) – thaJeztah

+0

它不通過AJAX提交,我的提交按鈕不會提交;那是不同的功能? –

相關問題