0

我正在处理一个动态添加几个字段的表单,这些输入字段也有快速验证,因此表单在验证后提交。如果不需要,也可以删除动态字段。我使用 jquery 删除了该字段,并且在表单上不再可用,但是执行相同的表单无法使用提交按钮提交。

我尝试使用 javascript form.submit() 函数提交表单,提交表单但无法接收按钮变量。

那么如何禁用对不可用字段的 spry 验证检查。


感谢您的回复,效果很好,请建议任何关于 spry 教程的参考 :)

此外,我想问我只使用以下内容来应用 spry 验证!

var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield_", "none", {minChars:5, maxChars:10});

但我只是破坏 sprytextfield1?用它作为还是必须使用其他方法好!

还提到如何在多个字段上实现......一些选择字段也在哪里!

4

1 回答 1

1

如果您有对要删除的元素的引用,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.

于 2011-09-18T16:48:55.793 回答