选择窗体上的Cakephp onChange事件(下拉)

i7uq4tfw  于 2022-11-12  发布在  PHP
关注(0)|答案(2)|浏览(143)

我有一个带有选择输入的表单。我想在选择下拉列表时自动提交表单。
我的代码:

<?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 )

我也尝试了'onChange'=〉' this.form.submit()',但是得到了同样的错误。
谁能帮帮忙。

holgip5t

holgip5t1#

您可以在表单中添加一个“id”属性,然后每次在“select”元素中获得“onchange”事件时,您都必须获取“id”值并将其传递给javascript函数“document.getElement('idValueHere ')”,然后调用函数submit。更清楚地说,下面的代码:

<?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');?>
bejyjqdl

bejyjqdl2#

希望对你有帮助。

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

相关问题