如何验证AMP表单中的复选框 [英] How to validate checkboxes in an AMP form
本文介绍了如何验证AMP表单中的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在AMP中有一个带有一组复选框的表单,例如:
I have a form in AMP that has a set of checkboxes, for example:
<form method="post" target="_blank" name="form" custom-validation-reporting="show-all-on-submit" action-xhr="/some-xhr-action">
<span visible-when-invalid="valueMissing" validation-for="favoriteSports">You must select at least ONE sport.</span>
<label for="favoriteSports">Choose one or more of your favorite sports:</label>
<input type="checkbox" required id="favoriteSports" name="favoriteSports" value="football">Football</input>
<input type="checkbox" required id="favoriteSports" name="favoriteSports" value="baseball">Baseball</input>
<input type="checkbox" required id="favoriteSports" name="favoriteSports" value="basketball">Basketball</input>
<input type="checkbox" required id="favoriteSports" name="favoriteSports" value="soccer">Soccer</input>
<input type="submit" value="Submit"></input>
</form>
如何验证是否已使用AMP选中了"festivalSports"复选框中的至少一个?
How can I validate that at least one of the favoriteSports checkboxes is checked using AMP?
推荐答案
我同意使用amp-bind将有助于解决您的问题.
I agree using amp-bind will help to solve your problem.
例如:
<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
<link rel="canonical" href="www.example.com">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
</head>
<body>
<amp-state id="pageStatus">
<script type="application/json">
{
"choiceChecked": 0
}
</script>
</amp-state>
<form method="post" target="_blank" name="form" custom-validation-reporting="show-all-on-submit" action-xhr="/some-xhr-action">
<span visible-when-invalid="valueMissing" validation-for="favoriteSports">You must select at least ONE sport.</span>
<label for="favoriteSports">Choose one or more of your favorite sports:</label>
<input type="checkbox" on="change:AMP.setState({pageStatus: {choiceChecked: event.checked == true ? pageStatus.choiceChecked + 1 : pageStatus.choiceChecked - 1}})" required id="favoriteSports" name="favoriteSports" value="football">Football</input>
<input type="checkbox" on="change:AMP.setState({pageStatus: {choiceChecked: event.checked == true ? pageStatus.choiceChecked + 1 : pageStatus.choiceChecked - 1}})" required id="favoriteSports" name="favoriteSports" value="baseball">Baseball</input>
<input type="checkbox" on="change:AMP.setState({pageStatus: {choiceChecked: event.checked == true ? pageStatus.choiceChecked + 1 : pageStatus.choiceChecked - 1}})" required id="favoriteSports" name="favoriteSports" value="basketball">Basketball</input>
<input type="checkbox" on="change:AMP.setState({pageStatus: {choiceChecked: event.checked == true ? pageStatus.choiceChecked + 1 : pageStatus.choiceChecked - 1}})" required id="favoriteSports" name="favoriteSports" value="soccer">Soccer</input>
<input type="submit" value="Submit"></input>
<p>Checked:
<span [text]="pageStatus.choiceChecked">0</span>
</p>
</form>
</body>
</html>
在该代码中,您有一个变量表示完成了多少次选择,然后可以将其与amp-bind一起使用,并在值为0时使提交按钮不可用(通过CSS).
In that code you have a variable representing how many selection have been done, you can then use it with amp-bind and make the submit button not available (via css) when the value is 0.
这篇关于如何验证AMP表单中的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文