使用Svelte中的<select>循环实现双向绑定

huangapple go评论64阅读模式
英文:

Two-way binding with <select> loop in Svelte

问题

考虑以下的 Svelte 代码(在此处尝试)。

当我更改下拉菜单的值,然后单击按钮调用 addFruit 时,fruits 的各个元素保持不变。

我如何为这些动态添加的元素实现绑定?

<script>
	let options = [
		{"id": "fruit1", "name": "Apple"},
		{"id": "fruit2", "name": "Banana"},
	];

	let fruits = [
		{"fruit_id": "fruit1"}
	];

	function addFruit() {
		fruits.push({ fruit_id: "fruit1" });
		fruits = fruits;
        console.log(fruits);
	}
</script>

{#each fruits as fruit, i}
	<select bind:value={fruit.fruit_id}>
		{#each options as option}
	  		<option value={option.id}>{option.name}</option>
	    {/each}
	</select>
{/each}

<button on:click={addFruit}>+</button>
英文:

Consider the following Svelte code (try it here).

When I change the value of a dropdown, and then click the button to call addFruit, then the individual elements of fruits remain unchanged.

How can I implement binding for these dynamically-added elements?

&lt;script&gt;
	let options = [
		{&quot;id&quot;: &quot;fruit1&quot;, &quot;name&quot;: &quot;Apple&quot;},
		{&quot;id&quot;: &quot;fruit2&quot;, &quot;name&quot;: &quot;Banana&quot;},
	];

	let fruits = [
		{&quot;fruit_id&quot;: &quot;fruit1&quot;}
	];

	function addFruit() {
		fruits.push({ fruit_id: &quot;fruit1&quot;});
		fruits = fruits;
        console.log(fruits);
	}
&lt;/script&gt;

{#each fruits as fruit, i}
	&lt;select value={fruit.fruit_id}&gt;
	{#each options as option}
	  &lt;option value={option.id}&gt;{option.name}&lt;/option&gt;
    {/each}
	&lt;/select&gt;
{/each}

&lt;button  on:click={addFruit}&gt;+&lt;/button&gt;

答案1

得分: 1

现在一切都是单向的(进入元素)。你必须像这样添加绑定

&lt;select bind:value={fruit.fruit_id}&gt;
英文:

Right now everything is one-way (into the elements). You have to add bindings like this:

&lt;select bind:value={fruit.fruit_id}&gt;

答案2

得分: -1

我尝试解决这个问题
你需要这个: 解决方案

代码:

&lt;script&gt;
	let options = [
		{&quot;id&quot;: &quot;fruit1&quot;, &quot;name&quot;: &quot;Apple&quot;},
		{&quot;id&quot;: &quot;fruit2&quot;, &quot;name&quot;: &quot;Banana&quot;},
		{&quot;id&quot;: &quot;fruit3&quot;, &quot;name&quot;: &quot;Mango&quot;},
		{&quot;id&quot;: &quot;fruit4&quot;, &quot;name&quot;: &quot;Coco&quot;},
	];

	let fruits = [
		{&quot;fruit_id&quot;: &quot;fruit1&quot;}
	];

	function addFruit() {
		// fruits.push({ fruit_id: kit});
		fruits = [...fruits,{ fruit_id: kit}];
		console.log(fruits);
	}
	let kit=options[0].id;
	$: console.log(kit);
&lt;/script&gt;

	&lt;select bind:value={kit}&gt;
	{#each options as option}
	  &lt;option value={option.id}&gt;{option.name}&lt;/option&gt;
  {/each}
	&lt;/select&gt;

&lt;button  on:click={addFruit}&gt;+&lt;/button&gt;
英文:

I tried for Solving this Issue
Do u need this : solution

code :

&lt;script&gt;
	let options = [
		{&quot;id&quot;: &quot;fruit1&quot;, &quot;name&quot;: &quot;Apple&quot;},
		{&quot;id&quot;: &quot;fruit2&quot;, &quot;name&quot;: &quot;Banana&quot;},
		{&quot;id&quot;: &quot;fruit3&quot;, &quot;name&quot;: &quot;Mango&quot;},
		{&quot;id&quot;: &quot;fruit4&quot;, &quot;name&quot;: &quot;Coco&quot;},
	];

	let fruits = [
		{&quot;fruit_id&quot;: &quot;fruit1&quot;}
	];

	function addFruit() {
		// fruits.push({ fruit_id: kit});
		fruits = [...fruits,{ fruit_id: kit}];
		console.log(fruits);
	}
	let kit=options[0].id;
	$: console.log(kit);
&lt;/script&gt;


	&lt;select bind:value={kit}&gt;
	{#each options as option}
	  &lt;option value={option.id}&gt;{option.name}&lt;/option&gt;
  {/each}
	&lt;/select&gt;

&lt;button  on:click={addFruit}&gt;+&lt;/button&gt;

huangapple
  • 本文由 发表于 2023年7月3日 22:56:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定