开发者

jquery submit on change with a twist

开发者 https://www.devze.com 2023-02-11 16:28 出处:网络
I have a form with a single select_field that gets submitted via jquery whenever the select field is changed.I use something like this:

I have a form with a single select_field that gets submitted via jquery whenever the select field is changed.I use something like this:

$开发者_高级运维('#scope').change(function() {
  this.form.submit();
});

Where scope is the ID of the select_field. This works as expected.

We want to change the view slightly where the select field is hidden until the user clicks a link => then it pops up and you can select as before.

The problem:

When the user clicks the link to show the select field. It also instantly triggers the change event somehow (even though the value of the select field has not changed).

Any easy way around this that I am overlooking?

Thanks for your time,

Erwin


you might want to register to the change event after you make the select-field visible

e.g.:

$(the_link_selector).click(function() {
    $('#scope')
       .show()
       .change(function () { this.form.submit(); });
});


jQuery(document).ready(function($) {
  $("#scope").hide();
    $("#link").click(function(event) {
      event.preventDefault();
      $("#scope").show();
  });
    $("#scope").change(function() {
        $("#testform").submit();
    });
});

demo link

0

精彩评论

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