[英]How to prevent a form submission if radio button is not clicked
var $form = $('#style');
var $radio = $('.changeprice');
$form.submit( function(e) {
if(!$radio.is(':checked')) {
alert('Please confirm!');
e.preventDefault();
}
});
這是我在文檔准備功能中擁有的JS代碼。
這是與HTML表單相關的代碼
<%= simple_form_for @order, url: wizard_path, :method => :put, id: 'style' do |f| %>
<input type="radio" id="1" class="changeprice" />
<input type="radio" id="2" class="changeprice" />
<%end %>
只需將required
的HTML 5標記放在一個單選按鈕上。 將其放在一個上可以使它在提交之前至少需要一組。 您的JS無需停止就可以停止提交表單。
<%= simple_form_for @order, url: wizard_path, :method => :put, id: 'style' do |f| %>
<input type="radio" id="1" class="changeprice" required />
<input type="radio" id="2" class="changeprice" />
<%end %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.