问题
I'm working on a form in which few fields are added dynamically, those input fields also have spry validations, so that form submit after validation. that dynamic fields can also be removed if not required. I removed that fields using jquery and no longer available on the form but doing the same form is unable to submit using submit button.
I try to submit form using javascript form.submit() function, form submitted but button variable couldn't received.
So How can I disable the spry validation check on the unavailable fields.
Thanks for your reply thats work well please suggest any reference for spry tutorials :)
moreover I wanna ask I apply spry validation by just using the following!
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield_", "none", {minChars:5, maxChars:10});
but I m just destroy sprytextfield1? is it good to use it as or have to use other method!
also mention how to implement on multiple fields... where some select fields are also!
回答1:
If you have a reference to the elements being removed, Spry validation widgets have a destroy() method.
The following code shows doing this in a trivial manner:
<!DOCTYPE html>
<html>
<head>
<title>Notifications</title>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css">
<script>
function clearValidation(){
if(sprytextfield1){
sprytextfield1.reset();
sprytextfield1.destroy()
}
}
function reapplyValidation(){
sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "none", {minChars:5, maxChars:10});
}
</script>
</head>
<body>
<form name="form1" method="post" action="">
<label for="sample"></label>
<span id="sprytextfield1">
<label for="myField"></label>
<input type="text" name="myField" id="myField">
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldMinCharsMsg">Minimum number of characters not met.</span><span class="textfieldMaxCharsMsg">Exceeded maximum number of characters.</span></span>
<input type="submit" name="submit" id="submit" value="Submit">
<input type="button" name="clear" id="clear" value="clear" onclick="clearValidation();" >
<input type="button" name="reapply" id="reapply" value="reapply" onclick="reapplyValidation();" >
</form>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "none", {minChars:5, maxChars:10});
</script>
</body>
</html>
Assuming you have the Spry files in the same location as this page looks for them, when you load the page in a browser, if you immediately click the submit button, you should see the validation message. Clicking the clear button will remove the validation (I also added a reset() call to clear the validation message, but if your fields and their validation message wrapper are removed from the page, then you may not need that part). Then clicking the submit button will allow the page to submit properly. If you load the page, then click submit (seeing the validation message) then click the clear button (also clearing the validation message), then click the Reapply button. Then the submit button should show the validation.
来源:https://stackoverflow.com/questions/7453556/spry-validation-prevents-the-form-submission-on-unavailable-fields