欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

鼠标 点击 表单元素 click 事件 select change onchange 事件 提交表单 form submit 有大用


2

How can I add an onchange handler to a dropdown list in Drupal? The dropdown list is added using hook_form(). I have to perform a function depending on the onchange function. Is there a way to do this?

                      
improve this question                            
edited Nov 18 '11 at 11:51                                
                                   
                               
Bert                                    
45.7k67786                                    
asked Nov 12 '11 at 11:43                                
                                   
                               
add a comment                
       

4 Answers 正确答案

activeoldestvotes                    
       
7

You can add a form like this:

 hook_form()
 {
   $form1["dropdown"] = array(
    '#type' => 'select',
    '#title' => t('Preview the page with themes available'),
    '#options' => $ptions,
    '#default_value' => 'defalut_value',
    '#attributes' => array('onchange' => "form.submit('dropdown')"),
 );
//Submit button:
$form1['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit '),
        '#attributes' => array('style' => 'display: none;'),
);
                       

Now you can add submit functionality using hook_submit().

improve this answer                            
edited Nov 18 '11 at 11:51                                
                                   
                               
Bert                                    
45.7k67786                                    
answered Nov 18 '11 at 11:08                                
                               
user840298
  • I'm using D7 and getting the single quotes around dropdown encoded to ' I suppose by check_plain. How do I avoid that? – Nic Cottrell Jun 2 '13 at 16:03                                    
  • Is this possible in D7 ?? – Hitesh Apr 21 '14 at 16:10                                    
add a comment                    
           
       
4

Here is the simple example using '#ajax' property

$form['select'] = array(
  '#type' => 'select',
  '#title' => 'Option #1',
  '#options' => $option,
  '#ajax' => array(
    // Call function that rebuilt other field
    'callback' => 'ajax_load_field',
    'method' => 'replace',
    // div to be get replace by function output
    'wrapper' => 'chart',
    'effect' => 'fade'
  ),
);
                   
improve this answer                            
edited Dec 9 '15 at 9:57                                
                                   
                               
leymannx                                    
2,92842436                                    
answered Oct 3 '12 at 10:47                                
                                   
                               
add a comment                    
       
1

If you generate another form field by using this drop down.

Then use AHAH for that.

$form['my_form_submit'] = array(

'#type' => 'submit',

'#value' => t('Submit'),
'#weight' => 1,
'#submit' => array('my_form_submit'),//none JS version
'#ahah' => array(
  'event' => 'click',
  'path' => 'mymodule/js',  //Your ajax function path
  'wrapper' => 'myform-wrapper',
  'method' => 'replace',
  'effect' => 'fade',
  'progress' => array(
    'type' => 'bar',
    'message' => t('Loading...')
  )      
),
                   
improve this answer                            
edited Nov 18 '11 at 12:05                                
                                   
                               
Prajila V P                                    
1,05121627                                    
answered Nov 18 '11 at 11:27                                
                                   
                               
add a comment                    
       
1

While I am sure that Nicholas is way past this issue by now, this may help some who are searching for a solution.

I'm using D7 and getting the single quotes around dropdown encoded to ' I suppose by >check_plain. >How do I avoid that? – Nicholas Tolley Cottrell Jun 2 at 16:03

I just found the "Drupal" way of doing this.

Step 1, set a variable to contain dropdown by using drupal_add_js:

drupal_add_js(array('mymodule' => array('varname' => 'dropdown')), 'setting');
                       

Step 2, add the attributes line as

'#attributes' => array('onchange' => "form.submit(Drupal.settings.mymodule.varname)"),
                   
improve this answer                            
answered Jul 2 '13 at 20:11                                
                                   
                               
add a comment                    
       

来自  https://stackoverflow.com/questions/8104222/how-do-i-add-an-onchange-handler-to-a-dropdown-list-in-d...




5

I have a simple form with method GET as follow...

function MYMODULE_form($form, &$form_state) {
  $form['#method'] = 'get';
  $params = drupal_get_query_parameters();
  $form['cat'] = array(
    '#type' => 'select',
    '#title' => t('Categories'),
    '#options' => $categories,
    '#default_value' => isset($params['cat']) ? $params['cat'] : '',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
}
                   

GET actuly bypasses the default submit and validation functions, which is ok for me. I want to submit this form without submit button, when value in 'Categories' field is changed.

function MYMODULE_form($form, &$form_state) {
  $form['#method'] = 'get';
  $params = drupal_get_query_parameters();
  $form['cat'] = array(
    '#type' => 'select',
    '#title' => t('Categories'),
    '#options' => $categories,
    '#default_value' => isset($params['cat']) ? $params['cat'] : '',
    '#ajax' => array(
      'callback' => 'MYMODULE_form_submit',
    ),
  );
}
                   

How should I do this?

                      
improve this question                            
asked Aug 18 '14 at 1:22                                
                                   
                               
add a comment                
       

2 Answers  正确答案                

activeoldestvotes                    
       
6

Here is how I was able to do this using attributes...

'#attributes' => array('onchange' => 'this.form.submit();'),
                       

So it was just simple ....

function MYMODULE_form($form, &$form_state) {
  $form['#method'] = 'get';
  $params = drupal_get_query_parameters();
  $form['cat'] = array(
    '#type' => 'select',
    '#title' => t('Categories'),
    '#options' => $categories,
    '#default_value' => isset($params['cat']) ? $params['cat'] : '',
    '#attributes' => array('onchange' => 'this.form.submit();'),
  );
}
                       

Now I am able to submit form when field is changed.

improve this answer                            
answered Aug 19 '14 at 18:00                                
                                   
                               
add a comment                    
           
       
2

Like this?

css:

MY_FORM_SELECTOR input[type="submit"] {display:none;}
                       

without #ajax property:

JS:

$('CAT_SELECT').change(function() {
   $(this).closest('form').submit();
});
                       

or with #ajax property on submit field:

$('CAT_SELECT').change(function() {
   $(this).closest('form').find('input[type=submit]').click();
});
                       

UPDATE:                        

  • Create a JS file in theme directory

  • Open MYTHEME.info file and add scripts[] = JSFILENAME.js

  • Open JS file and put it something like this:

code:

(function ($) {
  Drupal.behaviors.SomeUniqueName = {
    attach: function (context, settings) {
      $('CAT_SELECT').change(function() {
        $(this).closest('form').submit();
      });
    }
  };
})(jQuery);
                       

clear the cache, that's it;

ps:// if you want to submit the form with AJAX, this gist can help you

improve this answer                            
edited Aug 19 '14 at 11:40                                
answered Aug 18 '14 at 7:11                                
                                   
                               
  • can you tell me exactly what to do with JS. I am not good in it. – Tanvir Ahmad Aug 19 '14 at 11:02                                    
  • @TanvirAhmad comment could be long, that's why I updated the answer.. – xurshid29 Aug 19 '14 at 11:41                                    
  • Thanks for the detailed answer, appreciate. But I did it some other way. Answer below. – Tanvir Ahmad Aug 19 '14 at 17:58                                    
  • @TanvirAhmad Yes, I saw it. I love the classics:) – xurshid29 Aug 19 '14 at 18:45                                    
add a comment                    
       

来自 https://drupal.stackexchange.com/questions/126930/submit-form-when-field-value-changed            


           


           


           

OnChange表单提交                

发布者chrisdpucci                                                    

大家好,                                                                

我正在使用6.x API,并且在使用相当简单的工作方面遇到了一些麻烦。正如标题所说,我正在尝试使用select元素来提交表单onchange。                                                                

我添加了正确的JS代码,它似乎“工作”,但它实际上并没有通过drupal提交钩子提交表单。这就是我对代码的看法:                                                                

$form['jump_value'] = array(
  '#type' => 'select',
  '#default_value' => $currentAsset,
  '#options' => $assetValues,
  '#attributes' => array('onchange' => 'this.form.submit();'),
);
                                                               

我想也许我需要专门提交给提交功能,但我不确定如何做到这一点。任何帮助表示赞赏。                                                                

谢谢,                                                                

克里斯                                                                

⋅ 分类:Drupal 6.x                                                

评论                                                

                                               
yuriy.babenko的头像                                                    

你正在添加javascript                                                    

yuriy.babenko评论道                                                        

您正在将javascript提交调用添加到提交按钮的“表单”子项 - 该项目不存在。                                                                    

尝试:document.form-name.submit(); 
--- 
尤里Babenko 
www.yubastudios.com
我的Drupal教程:http://yubastudios.com/blog/tag/tutorials                                                                    

--- 
Yuriy Babenko | 技术顾问和高级开发人员
http://yuriybabenko.com                                                            

                                                   
chrisdpucci的头像                                                        

谢谢,你的建议                                                        

chrisdpucci评论道                                                            

谢谢,                                                                        

你的建议实际上并没有奏效,但它确实让我知道最终会起作用的尝试。                                                                        

为了防止将来遇到这种情况,我所做的就是:                                                                        

'onchange'=>'form.submit('{form_name}')'                                                                        

真的很简单,只需要让我的大脑朝着正确的方向碰撞。:)                                                                        

                                                       
yuriy.babenko的头像                                                            

很高兴你把它整理出来。                                                            

yuriy.babenko评论道                                                                

很高兴你把它整理出来。虽然有些奇怪但是没有用,因为它是原生的JavaScript ......你标签中有没有document.form-name.submit();name="form-name"form                                                                            

--- 
尤里Babenko 
www.yubastudios.com
我的Drupal教程:http://yubastudios.com/blog/tag/tutorials                                                                            

--- 
Yuriy Babenko | 技术顾问和高级开发人员
http://yuriybabenko.com                                                                    

                                                       
chaldar的头像                                                            

还需要提交按钮吗?                                                            

chaldar评论道                                                                

这很好,它适用于我。但是如果我删除了表单中提交的提交按钮(现在使用onchange是多余的),表单提交不会调用表单的提交功能。页面重新加载。新选择按预期持续存在。我错过了什么?这是D5.9。                                                                            

                                                           
yuriy.babenko的头像                                                                

尝试添加隐藏的表单                                                                

yuriy.babenko评论道                                                                    

尝试添加名为“op”且值为“Submit”的隐藏表单项。
--- 
尤里Babenko 
www.yubastudios.com
我的Drupal教程:http://yubastudios.com/blog/tag/tutorials                                                                                

--- 
Yuriy Babenko | 技术顾问和高级开发人员
http://yuriybabenko.com                                                                        

                                                               
chaldar的头像                                                                    

仍然是同样的问题:-(                                                                    

chaldar评论道                                                                        

我担心它仍然无效,除非我有一个'#type'=>'submit'表单元素。这是我的代码的骨架:                                                                                    

function mymodule_select() {
  $setting = variable_get('mymodule_setting', 1);
  $options = array('' => t('-Please select-'));
  if ($setting == 1) {
    $options[2] = t('Setting 2');
  } else {
    $options[1] = t('Setting 1');
  }
  $form['setting'] =
    array('#type' => 'select', '#title' => od_t('Setting'),
          '#options' => $options, '#default_value' => '',
          '#attributes' => array('onchange' => "form.submit('mymodule_select')"));
  $form['go'] = array('#type' => 'submit', '#value' => t('Go'));
  // $form['op'] = array('#type' => 'value', '#value' => t('Go'));
  // $form['op'] = array('#type' => 'hidden', '#value' => t('Go'));
  return $form;
}

function mymodule_select_submit($form_id, $form_values) {
  variable_set('mymodule_setting', $form_values['setting']);
}
                                                                                   

这按预期工作,onchange选择成功调用提交函数,并从提交返回表单更改中的选择选项。但是,如果我用任何注释掉的行替换提交按钮行,则onchange不再调用提交函数。我究竟做错了什么?提前致谢。                                                                                    

                                                                   
gforce301的头像                                                                        

明确地给它起名字                                                                        

gforce301评论道                                                                            

明确地给它op的名称属性。像这样'#name'=>'op'。                                                                                        

                                                                   
gforce301的头像                                                                        

更多                                                                        

gforce301评论道                                                                            

更确切地说:                                                                                        

$form['op'] = array (
  '#type' => 'hidden',
  '#value' => 'Submit',
  '#name' => 'op',
  '#id' => 'edit-submit',
);
                                                                                       

id属性可能是不必要的,但它使隐藏的'mimic'成为标准的提交按钮。                                                                                        

                                                                       
chaldar的头像                                                                            

这不起作用:-(                                                                            

chaldar评论道                                                                                

谢谢,但这仍然无效。我尝试了没有#id,仍然没有变化。                                                                                            

或许onchange submit是一个非常重要的功能,这需要Drupal文档中的一些文档?                                                                                            

                                                                   
yuriy.babenko的头像                                                                        

如果gforce发布了什么                                                                        

yuriy.babenko评论道                                                                            

如果发布的gforce对你不起作用,那就是最后的选择:用CSS隐藏提交按钮。您可以在一个CSS文件中手动定义此CSS,或者:                                                                                        

$form['go'] = array('#type' => 'submit', '#value' => t('Go'), '#attributes' => array('style', 'display: none;'));
                                                                                       

--- 
尤里Babenko 
www.yubastudios.com
我的Drupal教程:http://yubastudios.com/blog/tag/tutorials                                                                                        

--- 
Yuriy Babenko | 技术顾问和高级开发人员
http://yuriybabenko.com                                                                                

                                                                       
chaldar的头像                                                                            

这工作:-)                                                                            

chaldar评论道                                                                                

这最终成功了。我没有插入任何外部CSS,只是将display:none样式属性添加到按钮本身。非常感谢。                                                                                            

                                                                       
benoit.borrel的头像                                                                            

隐藏提交按钮代码                                                                            

benoit.borrel评论道                                                                                

Yuri Babenko犯了一个错字。                                                                                            

应该读一读:                                                                                            

<?php>
$form['go'] = array('#type' => 'submit', '#value' => t('Go'), '#attributes' => array('style' => 'display: none;'));
?>
                                                                                           

注意数组键'style'和值'display:none;'之间的'=>'。                                                                                            

干杯,                                                                                            

                                                                       
patiljivan的头像                                                                            

我在上面的代码中做了一些改动,工作正常                                                                            

patiljivan评论道                                                                                

感谢您提供意见                                                                                            

$ form ['submit'] = array(
'#type'=>'submit',
'#value'=> t('Go'),
'#attributes'=> array('style'=>'display:none ;')
);                                                                                            

                                                           
gforce301的头像                                                                

给你相同的答案                                                                

gforce301评论道                                                                    

在你发布同样问题的另一个帖子上给你相同的答案chaldar我在这里重新提交问题之前2小时给你答案。                                                                                

                                                               
yuriy.babenko的头像                                                                    

这是2008年 - 你不能指望                                                                    

yuriy.babenko评论道                                                                        

这是2008年 - 你不能指望人们阅读他们问题的答案;)                                                                                    

--- 
尤里Babenko 
www.yubastudios.com
我的Drupal教程:http://yubastudios.com/blog/tag/tutorials                                                                                    

--- 
Yuriy Babenko | 技术顾问和高级开发人员
http://yuriybabenko.com                                                                            

                                                                   
gforce301的头像                                                                        

哈哈                                                                        

gforce301评论道                                                                            

哈哈                                                                                        

                                                                       
chaldar的头像                                                                            

谢谢                                                                            

chaldar评论道                                                                                

感谢你们两位的回答。                                                                                            

对多个帖子的问题我表示歉意。我应该在再次发布之前检查第一个帖子。                                                                                            

                                                       
Biodroid的图片                                                            

不错的工作                                                            

Biodroid评论道                                                                

你的解决方案帮助我解决了我的问题,感谢你在这里做腿部工作:)                                                                            

                                               
lucsan的头像                                                    

onchange属性                                                    

lucsan评论道                                                        

经过一番相当混乱之后,我发现效率最高                                                                    

'#attributes' => array('onchange' => 'document.id-of-the-form.submit();'),
                                                                   

此方法不需要提交按钮,它将调用表单上的第一个提交按钮。
如果表单上没有提交按钮,则不会调用hook_form_submit(),但会调用hook_form_validate()。                                                                    

注意:从前面的示例document.form.submit() - 可能被解释为单词形式,表单名称等。我发现只有form-id工作。                                                                    

                                                   
aaronaverill的头像                                                        

关闭但不完全                                                        

aaronaverill评论道                                                            

在javascript中注意那些连字符。                                                                        

'#attributes' => array('onChange' => 'document.getElementById("id-of-the-form").submit();'),                                                                        

这仍然需要一个隐藏的提交按钮。                                                                        


                                                                       

来自  https://www.drupal.org/forum/support/module-development-and-code-questions/2008-08-06/onchange-form-...                                                                        


                                                                   


           


普通分类: