How to prevent form submission if there is not any option selected from dropdown
forms, haml, javascript, jquery, ruby-on-rails
Solution
Got the solution
Fiddle
$("#frm").submit(function(event){
// var valDDL = $(this).val();
//event.preventDefault();
var valDDL = $("#store_name").val();
if(valDDL=="")
{
event.preventDefault();
alert("select dropdown option");
}
});
HTML
<form method="post" id="frm">
<select id="store_name" class="select1" name="store[name]">
<option value="">Select Action</option>
<option value="delete">Delete</option>
<option value="changestatus">Change Status</option>
</select>
<input type="submit" value="Submit" name="commit"/>
</form>
Problem
I am using Rails 4 and in my app I have one form where I am passing form action via dropdown box. Now I want if there is not any option selected then form should not be submitted. below is my code ``` = form_for @store, {:url => remove_multiple_store_path} do |f| = f.select(:name, [['Delete', 'delete'], ['Change Status', 'changestatus']],{:include_blank=> 'Select Action'}) .......... .......... .......... = f.submit 'Submit' ``` I am getting in browser ``` <form method="post" action="/remove_multiple_store" accept-charset="UTF-8"> .... ...... <select id="store_name" class="select1" name="store[name]"> <option value="">Select Action</option> <option value="delete">Delete</option> <option value="changestatus">Change Status</option> </select> </div> <input type="submit" value="Submit" name="commit"> ``` I wish if there is not any option selected or `Select Action` is selected then form should not be submitted. For this I have searched but everything is I am getting as result its for PHP and I don't know ajax. So its very difficult for me to understand If you need more details then just inform me I will post... Can any one please help me... thanks in advance :)