开发者

Submit Form for select option.onClick

How would I submit a form when the user clicks an option using jQuery? I found one similar qu开发者_如何转开发estion using the datepicker text input but I'm not that familiar with jQuery so I can't seem to convert it to work for a select item.


On click of an <option>:

$('option').click(function ()
{
    $(this).closest('form').submit();
});

On change of a <select> (this is probably the one you want):

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

As @Guffa commented:

The click event on options doesn't work in all browsers. Safari for example does't trigger it.

...so you definitely want the second one.


The click event on options doesn't work in all browsers. Use the change event of the select element.

Example:

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

Note that the submit event of the form is not triggered when you call the submit method to post the form.


A small correction of these answers:

$(document).ready(function() {
    $('#some_link').click(function() {
        $('#form_id').submit();
    });
});


$('select').change(function() {
  $('form').submit();
});


$('#option').click(function () {
    // form validation and such then

    $('form').submit();
})

This is the easiest way i can think of

UPDATE:

for specific form, you can use its name or id, it really depends on how your html looks like

$('form[name="formnamehere"]').submit();


I found the other answers helpful but to truly achieve what I needed of individual actions based on each option click this was exactly what I needed:

jquery: option plus class and click

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜