2013-03-28 127 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提交,我的提交按钮不会提交;那是不同的功能? –

相关问题