簡體   English   中英

選擇表單上的 Cakephp onChange 事件(下拉菜單)

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

我有一個帶有選擇輸入的表單。 我想在選擇下拉列表時自動提交表單。

我的代碼:

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

希望對你有幫助。

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