1

How do I make :confirm=>"Are you sure" conditional? I have this code

<div id="container"><%= form_for @question,:remote=>true,:html=>{:name=>"question"} do |f|%>
    <p>
        <div class ="row">
        <%= label_tag(:topic,@displayquestion.question_text,:class=>"form-label")%>
        </div>
    </p>
    <p>
    <label>
    <%= f.radio_button(:answer, "1") %>
    <%= label_tag(:choice1,@displayquestion.choice1,:class=>"form-label")%>
    </label>
    </p>
    <p>
    <label>
    <%= f.radio_button(:answer, "2") %>
    <%= label_tag(:choice2,@displayquestion.choice2,:class=>"form-label")%>
    </label>
    </p>
    <p>
    <label>
    <%= f.radio_button(:answer, "3") %>
    <%= label_tag(:choice3,@displayquestion.choice3,:class=>"form-label")%>
    </label>
    </p>
    <p>
    <label>
    <%= f.radio_button(:answer, "4") %>
    <%= label_tag(:choice4,@displayquestion.choice4,:class=>"form-label")%>
    </label>
    </p>
    <%= f.submit(:value=>next,:class =>"button",:disable_with=>'loading') %>
<% end %></div>

I want to make it such that it will prompt only if no radio button is selected. I have this jQuery code which will prompt the user if no radio button was selected.

$(document).ready(function(){

   $("#container").on("click", ".edit_question", function(event){

     if (!$("input[@name='question[answer]']:checked").val()) {
       confirm('Are sure you want to submit a blank answer?');
    }

   if (!$("input[@name='answer[]']:checked").val()) {
       confirm('Are sure you want to submit a blank answer?');
    }

}
);
});

The problem is that even when I click cancel, the ajax request is sent, how do intercept the ajax request to call the jQuery code?

Thanks for the pointer! I did it this way and it works!

$(document).ready(function(){

   $("#container").on("submit", ".edit_question", function(event){
    var reply = true;
     if (!$("input[@name='question[answer]']:checked").val()) {
       reply=confirm('Are sure you want to submit a blank answer?');
    }

   if (!$("input[@name='answer[]']:checked").val()) {
       reply=confirm('Are sure you want to submit a blank answer?');
    }
   return reply;
}
);
});

1 Answer 1

2

Your click function needs to return false to cancel the page submission. confirm() returns a bool, true for ok, false for cancel.

Example:

$("#container").on("click", ".edit_question", function(event){

    if (!$("input[@name='question[answer]']:checked").val()
        || !$("input[@name='answer[]']:checked").val()  ) {
        return confirm('Are sure you want to submit a blank answer?');
    }
    return true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yours is shorter and nicer, will use yours. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.