I have a form something like below
<html>
<script>
$(document).ready(function(){
// find all the hidden input elements with error_check class
var error_elements = $('.error_check')
// Loop through each element find if the value equals 'california'
error_elements.each(function(){
if (element.value == 'california')
// prevent for submission and display alert
alert("cannot submit tax for this state")
else
// submit form
});
)};
</script>
<body>
<form action="{% url 'collect_salextax' %}" method="POST" id="states_form" class="form-horizontal">
<div class="control-group">
{% for state in states %}
<p>
<input id="error_{{state.0}}" type="hidden" name="{{state.0}} value="{{state.name}}" class="error_check"/>
<input id="{{state.0}}" type="text" name="{{state.0}}" class="value_check"/>
</p>
{% endfor %}
</div>
<div class="span11 pagination-centered marg_tp38">
<input name="" type="submit" class="btn btn-large big_btn " value="Submit">
</div>
</form>
<body>
</html>
So from the above as u can see i am generating the fields in a paragraph using a loop, and the value of hidden field will be state name from the array/list
So when i click on the submit button, its successfully going to form action attribute and returning the result
So can anyone please let me know how to do this very clearly in jquery ?
But what i am trying is, when a user clicks on submit
button
hidden input elements
with class error_check
"California"
, then display an alert like "You cannot submit tax
for california"you can do it like this:
$("#states_form").submit(function(event) {
$(".error_check").each(function(idx, obj) {
if ($(obj).val() == 'california') {
alert("cannot submit tax for this state");
event.preventDefault();
return false;
}
else {
// submit form
return true;
}
});
});