Can I Force Jquery Dirtyform to See A Form As Dirty?
We are using the JQuery dirtyform plugin to display a warning to users if they change form data and try to navigate away from the page and it works well.
One additional thing I would like to do is to force a form to be dirty when the page first loads. I can't figure out how to do this - I've attempted using javascript to change values on the form fields, I've tried adding the "changed" class to form fields, but neither of these trigger the warning dialog when I navigate away from the page.
Is there a way to do this?
ANSWE开发者_Python百科R:
$(document).ready(function() {
$("form").dirty_form({includeHidden: true});
$("a").dirty_stopper();
$("#dummy").val("1");
$("#dummy").blur();
});
and in the html:
<input type="hidden" name="dummy" id="dummy" value="0" />
$("#myFormID").find("input").change();
You'll obviously need to change the selectors, but I think this is what you want. It just triggers jQuery's "change" event (which is what Dirty Form is listening for) on the selected elements.
I solved this without having to use hidden fields by adding this function to the plugin:
$.fn.set_form_dirty = function(){
$(this).data("dirty", true);
};
Then I call this from the pages that I want to be dirty from start by adding this piece of code (in this case setting all forms on the page to dirty):
<script type="text/javascript">
$(document).ready(function() {
$("form").set_form_dirty();
});
</script>
Have you tried to apply the class "changed" to an input?
Whenever an input value changes it will get the 'changed' class added to it
精彩评论