简体   繁体   English

选择表单上的 Cakephp onChange 事件(下拉菜单)

[英]Cakephp onChange event on select form (dropdown)

I have a form with select input.我有一个带有选择输入的表单。 I want to auto submit the form when the dropdown list was selected.我想在选择下拉列表时自动提交表单。

My code:我的代码:

<?php echo $this->Form->create('Product', array('controller'=>'products', 'action'=>'shipping_area'));
    echo $this->Form->input('area', array('options' => array('1' => 'Within Malaysia', '2' => 'International'), 'empty' => 'choose area',
    'label' => 'Choose shipping area', 'onChange'=>'javascript:this.form.submit()'));
    //echo $this->Form->end('Save');
?>

I put 'onChange'=>'javascript:this.form.submit()', but it goes to http://localhost/cake/cake/products/shipping_area ( supposely http://localhost/cake/products/shipping_area )

I also tried 'onChange'=>'this.form.submit()', but got same error.我也试过'onChange'=>'this.form.submit()',但得到了同样的错误。

can anyone please help.任何人都可以请帮忙。

You can add an "id" attribute to the form and then every time that you get an "onchange" event in the "select" element, you have to obtain the "id" value and pass it to the javascript function "document.getElement('idValueHere')" and call to the fuction submit.您可以向表单添加“id”属性,然后每次在“select”元素中获得“onchange”事件时,您必须获取“id”值并将其传递给javascript函数“document.getElement” ('idValueHere')" 并调用函数提交。 More clearly, the following code:更清楚的是,下面的代码:

<?php 
# step 1: add an id attribute ('id' => 'anyFormName') to the array options

# step 2: add an onchange envent to the dropdown ('onChange'=>'document.getElementById("anyFormName").submit();')

echo $this->Form->create('Product', array('controller'=>'products', 'action'=>'shipping_area', 'id' => 'anyFormName'));
echo $this->Form->input('area', array('options' => array('1' => 'Within Malaysia', '2' => 'International'), 'empty' => 'choose area',
    'label' => 'Choose shipping area', 'onChange'=>'document.getElementById("anyFormName").submit();'));
//echo $this->Form->end('Save');?>

Hope it helps you.希望对你有帮助。

echo $this->Form->create('Product', array(
    'url' => array(
        'controller'=>'products', 'action'=>'shipping_area'
    )
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM