开发者

Why is jQuery, Rails and Chrome submitting sporadically?

开发者 https://www.devze.com 2023-02-09 17:19 出处:网络
So many problems trying to get a text link to submit a form in Chrome using Rails and jQuery. I have tried many combinations and the one that seemed to work best in Chorme is:

So many problems trying to get a text link to submit a form in Chrome using Rails and jQuery.

I have tried many combinations and the one that seemed to work best in Chorme is:

$('form:first').trigger('submit')

It's crazy... Why would it work on the first time and second and sometimes third time, but then stop working?

Here is the Rails code that renders the text link submit button:

<li><%= link_to raw("<span class='button approve'><span><span>SAVE</span></span></span>"), "index_users", :onclick=>"$('form:first').trigger('submit')"%></li>

I tried this code too with the same type of sporadic results in Chrome:

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

Any help is hugely appreciated.

Edited:

This is the what the form tag renders to:

<form method="post" id="form1" data-remote="true" class="search_form general_form" action="/settings/2/update_user" accept-charset="UTF-8">

And here is what the text button renders to:

<li><a onclick="$(this).trigger('click');" href="index_users"><span class="button approve"><span><span>SAVE</span></span></span></a></li>
开发者_JAVA百科

The onclick code is changing since I am trying everything I can find. Thanks again for your advice.


Little confusing to see what you are trying to do with link_to raw. Why don't you first do something real simple with a submit form? I don't see a form or the action to that is serving the submit. etc


I ended up using this

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

and I think the other key was to us "#"

  • <%= link_to raw("SAVE"), "#", :onclick=>"$(this).closest('form').submit();"%>

  • I suspect your problem is related to Javascript event listeners - hanging them correctly for UJS (which I assume you're invoking with the data-remote="true" attribute on your form) can be tricky.

    What does your controller render on a user update? HTML? Javascript?

    0

    精彩评论

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