gpt4 book ai didi

javascript - 如何验证具有必需属性的 paper-radio-group 元素

转载 作者:行者123 更新时间:2023-12-03 06:46:33 28 4
gpt4 key购买 nike

这与 this question 类似的问题但没有得到答复并且是 1.0 之前的版本。
我需要确保在提交表单时至少选择一个 paper-radio-button
我可以为每个 paper-radio-button 放置一个必需的属性,但这意味着必须选择每个单选按钮。

<iron-label> Type<br/>
<paper-radio-group name="types" allow-empty-selection attr-for-selected="value" selected="{{model.type}}" noink >
<paper-radio-button name="type" value="home" required> Home</paper-radio-button>
<paper-radio-button name="type" value="work" required> Work</paper-radio-button>
<paper-radio-button name="type" value="portal" required> Portal</paper-radio-button>
<paper-radio-button name="type" value="shipping" required> Shipping</paper-radio-button>
<paper-radio-button name="type" value="billing" required> Billing</paper-radio-button>
<paper-radio-button name="type" value="other" required> Other</paper-radio-button>
</paper-radio-group>
</iron-label>

最佳答案

与其让用户选择不选择任何单选按钮,不如强制选择。您可以通过删除 allow-empty-selection 来做到这一点来自 paper-radio-group 和来自 paper-radio-buttonrequired 属性。我还建议使用 fallback-selection paper-radio-group 上,这样 model.type 中的意外值将导致默认值,例如“其他”。

<head>
<base href="https://polygit.org/polymer+1.11.0/components/">
<script src="webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="iron-form/iron-form.html">
<link rel="import" href="iron-label/iron-label.html">
<link rel="import" href="paper-radio-group/paper-radio-group.html">
<link rel="import" href="paper-radio-button/paper-radio-button.html">
<link rel="import" href="paper-button/paper-button.html">
</head>
<body>
<x-form></x-form>

<dom-module id="x-form">
<template>
<iron-form id="form">
<form action="">
<iron-label> Type<br/>
<paper-radio-group name="types" attr-for-selected="value" fallback-selection="other" selected="{{model.type}}" noink>
<paper-radio-button name="type" value="home"> Home</paper-radio-button>
<paper-radio-button name="type" value="work"> Work</paper-radio-button>
<paper-radio-button name="type" value="portal"> Portal</paper-radio-button>
<paper-radio-button name="type" value="shipping"> Shipping</paper-radio-button>
<paper-radio-button name="type" value="billing"> Billing</paper-radio-button>
<paper-radio-button name="type" value="other"> Other</paper-radio-button>
</paper-radio-group>
</iron-label>
<div>
<paper-button on-click="submit">Submit</paper-button>
</div>
</form>
</iron-form>
</template>
<script>
HTMLImports.whenReady(function() {
Polymer({
is: 'x-form',
properties : {
model: {
type: Object,
value: function() { return {type: 'fake'}; }
}
},
submit: function() {
console.log('type', this.model.type);
}
});
});
</script>
</dom-module>
</body>

codepen

关于javascript - 如何验证具有必需属性的 paper-radio-group 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37714472/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com