这是与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>
答案 0 :(得分:1)
不是给用户选择不选择任何单选按钮,而是强制选择。您可以通过从paper-radio-group
移除allow-empty-selection
和从required
移除paper-radio-button
属性来执行此操作。我还建议在paper-radio-group
上使用fallback-selection
,以便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>