forms - JQuery selectors fine point -
was working on website involved simple form submittals. rather handle submittals classic, old-school html way (i.e. form tag, "action=" attribute, button, etc.), decided submit data via jquery's .ajax() method.
handling things way, necessary explicitly clear form fields, after "submittal". let's actual form looked this:
<form action="some_page.asp" id="my_form" method="post"> <input type="checkbox" name="form_field" value="some_value" /> <input type="checkbox" name="form_field" value="some_other_value" /> <input type="checkbox" name="form_field" value="yet_another_value" /> <input type="text" name="form_field" /> <input type="hidden" name="form_field" class="must_not_clear" value="" /> <input type="hidden" name="form_field" class="must_clear" value="" /> <input type="hidden" name="form_field" class="must_clear" value="" /> </form>
now, though form has classic "action" attribute, i'm not using that. i'm submitting data via jquery .ajax(), , therefore have clear, or reset form fields after "submittal".
so, real question...how come this code:
$("#my_form").find("input:checkbox").removeattr("checked"); $("#my_form").find("input:text").val(""); $("#my_form").find("input:hidden[class=must_clear]").val("");
successfully clears right fields whereas this:
$("#my_form").find("input:checkbox").removeattr("checked"); $("#my_form").find("input:text").val(""); $(".must_clear").val("");
does not?
when resetting form cycle through fields , reset them individually or reset form itself:
$('#myform')[0].reset();
this may not direct answer question though might helpful
edit: if have fields not want reset move them out of <form>
you're gathering data manually anyway. if leave fields need reset inside <form>
resetting form pretty easy
Comments
Post a Comment