1
我該如何做:確認=>「你確定」有條件嗎? 我有這樣的代碼如何在Ruby on Rails中創建ajax條件確認?
<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>
我想讓它這樣,如果沒有選擇單選按鈕,它只會提示。 我有這個jQuery代碼將提示用戶如果沒有單選按鈕被選中。
$(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?');
}
}
);
});
的問題是,即使當我點擊取消,Ajax請求被髮送,如何攔截Ajax請求調用jQuery代碼?
感謝您的指針! 我這樣做,它的工作原理!
$(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;
}
);
});
此致較短和更好,將使用你的。謝謝! – user1533398