문제

Rails uses unobtrusive javascript to prompt confirm messages on a (submit) button. These messages are stored in a data-confirm attribute on the button/link.

Now, suppose you only want to confirm sometimes, as in my case where I only want to prompt when busy editing prices, you obviously add and remove these attributes with javascript (jQuery)

$('.create-order').attr('data-confirm', "Warning: You are busy editing item prices and you haven't applied your changes yet. Continue?");

and

$('.create-order').removeAttr('data-confirm');

This is working and the attribute is definitely being appended and removed ( I double checked). But I noticed that once the message has been displayed once, it will display every time after that as well, even after the attribute has been removed. ( I invite anyone to try this as well and share your results? )

How can I stop this from happening?

도움이 되었습니까?

해결책

In your case I would override the confirm method from jquery_ujs.js file as following :

$.rails.confirm  = function(message){
 //your logic here : must return true or false, you can call js default confirm method
}

다른 팁

This is how I deal with it (seems to be simplier) :

Rails form :

<%= @form_for(@obj) do |f| %>
    <%= f.text_field :attribute %><br />
    <%= f.radio_button :my_radio, false %> Finished <%= f.radio_button :my_radio, true %> Not yet !<br />
    <%= f.submit id: "submit" %>
<% end %>

And Jquery :

<script>
    $("#submit").click(function() {
        if ($("#my_radio_false").is(':checked')) {
            return confirm("Are-you sure ?");
            return true;
        }
    });
</script>

You can test it here :

$("#submit").click(function() {
  if ($("#my_radio_false").is(':checked')) {
    return confirm("Are you sure ?");
    return true;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="text" /><br />
  <input value="true" name="analysis[is_analysed]" id="my_radio_true" type="radio"> Finished 
  <input value="false" name="analysis[is_analysed]" id="my_radio_false" type="radio" checked="checked"> Not Yet !<br />
  <input name="commit" value="Test it !" id="submit" type="submit">
</form>

Hope that helps

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top