<label> <inputtype=checkboxbind:checked={yes}> Yes! Send me regular email spam </label>
{#if yes} <p>Thank you. We will bombard your inbox and sell your personal details.</p> {:else} <p>You must opt in to continue. If you're not paying, you're the product.</p> {/if}
<buttondisabled={!yes}> Subscribe </button>
四、输入框组
如果你需要绑定更多值,则可以使用bind:group 将 value 属性放在一起使用。 在bind:group中,同一组的单选框值时互斥的,同一组的复选框会形成一个数组。
添加bind:group 到每一个选择框:
1
<inputtype=radiobind:group={scoops}value={1}>
在这种情况下,我们可以给复选框标签添加一个 each 块来简化代码。 首先添加一个menu变量到 <script>标签中,然后使用each继续修改
<script> let scoops = 1; let flavours = ['Mint choc chip'];
let menu = [ 'Cookies and cream', 'Mint choc chip', 'Raspberry ripple' ];
functionjoin(flavours) { if (flavours.length === 1) return flavours[0]; return`${flavours.slice(0, -1).join(', ')} and ${flavours[flavours.length - 1]}`; } </script>
<h2>Size</h2>
<label> <inputtype=radiobind:group={scoops}value={1}> One scoop </label>
<label> <inputtype=radiobind:group={scoops}value={2}> Two scoops </label>
<label> <inputtype=radiobind:group={scoops}value={3}> Three scoops </label>
<h2>Flavours</h2>
{#each menu as flavour} <label> <inputtype=checkboxbind:group={flavours}value={flavour}> {flavour} </label> {/each}
{#if flavours.length === 0} <p>Please select at least one flavour</p> {:else if flavours.length > scoops} <p>Can't order more flavours than scoops!</p> {:else} <p> You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'} of {join(flavours)} </p> {/if}
现在可以轻松地将我们的冰淇淋菜单扩展到令人兴奋的新方向。
REPL
五、多行纯文本-textarea
例子:
1 2 3 4 5 6 7 8 9 10 11 12 13
<script> import { marked } from'marked'; let value = `Some words are *italic*, some are **bold**`; </script>
<script> let questions = [ { id: 1, text: `Where did you go to school?` }, { id: 2, text: `What is your mother's name?` }, { id: 3, text: `What is another personal fact that an attacker could easily find with Google?` } ];
let selected;
let answer = '';
functionhandleSubmit() { alert(`answered question ${selected.id} (${selected.text}) with "${answer}"`); } </script>
<script> let scoops = 1; let flavours = ['Mint choc chip'];
let menu = [ 'Cookies and cream', 'Mint choc chip', 'Raspberry ripple' ];
functionjoin(flavours) { if (flavours.length === 1) return flavours[0]; return`${flavours.slice(0, -1).join(', ')} and ${flavours[flavours.length - 1]}`; } </script>
<h2>Size</h2>
<label> <inputtype=radiobind:group={scoops}value={1}> One scoop </label>
<label> <inputtype=radiobind:group={scoops}value={2}> Two scoops </label>
<label> <inputtype=radiobind:group={scoops}value={3}> Three scoops </label>
<h2>Flavours</h2>
<selectmultiplebind:value={flavours}> {#each menu as flavour} <optionvalue={flavour}> {flavour} </option> {/each} </select>
{#if flavours.length === 0} <p>Please select at least one flavour</p> {:else if flavours.length > scoops} <p>Can't order more flavours than scoops!</p> {:else} <p> You ordered {scoops} {scoops === 1 ? 'scoop' : 'scoops'} of {join(flavours)} </p> {/if}
<script> // These values are bound to properties of the video let time = 0; let duration; let paused = true;
let showControls = true; let showControlsTimeout;
functionhandleMousemove(e) { // Make the controls visible, but fade out after // 2.5 seconds of inactivity clearTimeout(showControlsTimeout); showControlsTimeout = setTimeout(() => showControls = false, 2500); showControls = true;
if (!(e.buttons & 1)) return; // mouse not down if (!duration) return; // video not loaded yet
const { left, right } = this.getBoundingClientRect(); time = duration * (e.clientX - left) / (right - left); }
functionhandleMousedown(e) { // we can't rely on the built-in click event, because it fires // after a drag — we have to listen for clicks ourselves
functionhandleMouseup() { if (paused) e.target.play(); else e.target.pause(); cancel(); }