我有一个带有选择输入的表单。我想在选择下拉列表时显示警告。 视图:
<?php
echo $this->Form->create('Office');
echo $this->Form->input('under_office', array('type' => 'select', 'options' => $settings,'empty' => false));
echo $this->Form->end('Save');
?>
控制器
public function add() {
$settings = $this->Office->Officetype->find('list', array('fields'=> array('Officetype.id', 'Officetype.name')
));
$this->set(compact('settings'));
if ($this->request->is('post')) {
$this->Office->create();
$post_data = $this->request->data;
if ($this->Office->save($post_data)) {
$this->Session->setFlash(__('Office details saved successfully'));
return $this->redirect(array('action' => 'add'));
}
$this->Session->setFlash(__('Unable to save Office details'));
}
}
答案 0 :(得分:1)
这可能对您有所帮助,首先在您的选择标记上添加一些ID。
echo $this->Form->input('under_office', array('type' => 'select', 'id' => 'myselect', 'options' => $settings,'empty' => false));
在执行上述代码之后,将代码放在<script> </script>
标记之间或您的javascript存在的位置之下,
document.getElementById('myselect').onchange = function (){
alert('hello guys');
}