Submit Form dropdown with jQuery

泪湿孤枕 提交于 2020-01-15 10:03:02

问题


I am using a dropdown plugin called dropkick, which replaces select menus with better looking, custom dropdowns. I want to display my list of WordPress categories in this dropdown. Here is the PHP code that generates the dropdown:

<form action="<?php bloginfo('url'); ?>/" method="get" class="pretty">
    <?php $select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
    echo $select; ?>
</form>

The JavaScript code to turn this into a "pretty dropdown" is as follows.

$('.pretty select').dropkick({
      theme: 'default',
      change: function (value, label) {
         INSERT CALLBACK HERE
      }
});

I need to write a callback that will submit the form after the user selects a category. How would I go about this?

I tried using return this.form.submit() and also return $(this).parent().form.submit() but that did not submit the form. How can I auto submit a form using jQuery?

The form action and method are already defined in the form.


回答1:


You can use jQuery's .submit() to post the form.

Given that you only have one form in the DOM with the class pretty, you could post the form like this:

$("form.pretty").submit();



回答2:


Christofer Eliasson's answer may be correct, but just in case you have more than one form, this would be better:

$(this).closest('form').submit();


来源:https://stackoverflow.com/questions/10487164/submit-form-dropdown-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!