如果您有对要删除的元素的引用,Spry 验证小部件有一个 destroy() 方法。
以下代码以一种简单的方式显示了这样做:
<!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.