如何正确地在Svelte中动态添加和删除文本输入字段?

发布于 2025-02-08 10:47:46 字数 1618 浏览 2 评论 0 原文

我是Svelte和Web开发的新手,希望您可以将我指向正确的方向。

当前代码属于Svelte组件。 如下所示:

默认情况下应该显示一个文本字段,同时允许在动态添加的按钮上添加并删除附加的文本字段。 当前,此代码可以动态添加文本字段,但是,它无法在按钮点击上动态删除文本字段。

我相信getDynamicElement函数可能会出现错误。但是,我不确定到底在哪里。有什么建议吗?

PS我知道这里有一些答案很接近,但我认为它们不适用于这种情况,尤其是在Svelte上。

<script>
  var num_links = 1;
  let container;

  const GetDynamicElement = (value) => {
    return (
      '<input name = "DynamicField" type="text" size =111  id =link  placeholder="Enter Next link! " value =  "' +
      value +
      '" />' +
      '<input type="button" value="Remove" on:click = {RemoveField(this)}>'
      // "RemoveSuggestionCart(this)" />'
    );
  };

  const addField = () => {
    if (num_links < 2) {
      console.log("addField");
      const div = document.createElement("DIV");
      div.innerHTML = GetDynamicElement("");
      container.appendChild(div); // Append timetable space
      num_links += 1;
    }
  };

  //Removes the entire division inclusive of it's text field.
  const RemoveField = (div) => {
    console.log("RemoveField");
    div.removeChild(div.parentNode);
    num_links -= 1;
  };
</script>



<div>
  <input
    name="DynamicField"
    type="text"
    size="121"
    id="link"
    placeholder="Enter First Link!"
  />
  <div bind:this={container} />
</div>

<button on:click|preventDefault={addField}>[+ add timetable link]</button>

<style>
</style>

I am new to Svelte and web development and I hope you could point me in the right direction.

The current code belongs in a svelte component.
As shown here: https://svelte.dev/repl/f0e5c30117724ec38b7d19781d2c4de6?version=3.48.0

It is supposed to show one text field by default, while allowing for an additional text field to be added and removed on dynamically added buttons.
Currently, this code can dynamically add the text field, however, it cannot dynamically remove the text field on button click.

I believe there might be an error in the GetDynamicElement function. However, I am not sure where exactly. Any suggestions?

p.s. I know there are answers here that are close, but I don't think they are applicable in this situation, especially on Svelte.

<script>
  var num_links = 1;
  let container;

  const GetDynamicElement = (value) => {
    return (
      '<input name = "DynamicField" type="text" size =111  id =link  placeholder="Enter Next link! " value =  "' +
      value +
      '" />' +
      '<input type="button" value="Remove" on:click = {RemoveField(this)}>'
      // "RemoveSuggestionCart(this)" />'
    );
  };

  const addField = () => {
    if (num_links < 2) {
      console.log("addField");
      const div = document.createElement("DIV");
      div.innerHTML = GetDynamicElement("");
      container.appendChild(div); // Append timetable space
      num_links += 1;
    }
  };

  //Removes the entire division inclusive of it's text field.
  const RemoveField = (div) => {
    console.log("RemoveField");
    div.removeChild(div.parentNode);
    num_links -= 1;
  };
</script>



<div>
  <input
    name="DynamicField"
    type="text"
    size="121"
    id="link"
    placeholder="Enter First Link!"
  />
  <div bind:this={container} />
</div>

<button on:click|preventDefault={addField}>[+ add timetable link]</button>

<style>
</style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橙味迷妹 2025-02-15 10:47:46

添加/删除字段,并有一个按钮进行日志或发送到端点或使用“日志”按钮的任何内容。

    <script>
    // to display one empty inputs before clicking add needed
    let values=[{
       "url": "",
       "name": "",
    }]; 
        
    const addField = () => {
       values = [...values, {url: '', name: ''}]
    };
        
    const removeField = () => {
       values = values.slice(0, values.length-1)
    };
    </script>
        
    {#each values as v, i}
       <div>
          <input id={i} type="text" bind:value={values[i].url} placeholder="url"/>
          <input id={i} type="text" bind:value={values[i].name} placeholder="name"/>
       </div>
    {/each}
{#if values.length >= 2}
   <input type="button" value="Remove" on:click={removeField}>
{/if]
<button on:click|preventDefault={addField}>Add</button>
<button on:click={() => console.log(values)}>Log Me</button>

尝试:

编辑:谢谢@Corrl-编辑。

Add/ remove fields and have a button to log or send to endpoint or whatever with "Log" button.

    <script>
    // to display one empty inputs before clicking add needed
    let values=[{
       "url": "",
       "name": "",
    }]; 
        
    const addField = () => {
       values = [...values, {url: '', name: ''}]
    };
        
    const removeField = () => {
       values = values.slice(0, values.length-1)
    };
    </script>
        
    {#each values as v, i}
       <div>
          <input id={i} type="text" bind:value={values[i].url} placeholder="url"/>
          <input id={i} type="text" bind:value={values[i].name} placeholder="name"/>
       </div>
    {/each}
{#if values.length >= 2}
   <input type="button" value="Remove" on:click={removeField}>
{/if]
<button on:click|preventDefault={addField}>Add</button>
<button on:click={() => console.log(values)}>Log Me</button>

Try: https://svelte.dev/repl/2441993f8d9946aa894bf07a8a8f9b4f

Edited: thanks @Corrl - edited nicer.

橙幽之幻 2025-02-15 10:47:46

您可以使用 num_link 和Svelte's > 使用Svelte创建输入:

{#each Array(num_links) as _, i}
<div>
  <input
    name="DynamicField"
    type="text"
    size="121"
    id={`link_${i}`}
    placeholder="Enter First Link!"
  />
</div>
{/each}

工作示例:

You can use your num_link and svelte's #each to create to inputs using svelte:

{#each Array(num_links) as _, i}
<div>
  <input
    name="DynamicField"
    type="text"
    size="121"
    id={`link_${i}`}
    placeholder="Enter First Link!"
  />
</div>
{/each}

Working example: https://svelte.dev/repl/04169e030b6944258cfd07af15873b48?version=3.48.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文