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;
}
);
});