gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:38:07 26 4
gpt4 key购买 nike

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

我的代码:

<?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()',但遇到了同样的错误。

谁能帮忙。

最佳答案

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

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

关于javascript - 选择表单(下拉菜单)上的 Cakephp onChange 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20833558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com