简体   繁体   English

提交表单后清除 svelte 中的输入字段

[英]Clear input field in svelte after form submission

I'm struggling to find a way to clear form inputs after submission.我正在努力寻找一种在提交后清除表单输入的方法。 I should mention that this form is in a Modal.我应该提一下,这种形式是模态的。 It posts to Supabase correctly, I just can't figure out how to clear the input fields.它正确发布到 Supabase,我只是不知道如何清除输入字段。 Any ideas?有任何想法吗? I'm new to this and still learning.我是新手,仍在学习。 Thank you.谢谢你。

My Code:我的代码:

<script>
const addVendorTest = async (event) => {
    const formData = new FormData(event.target)
   
    for(let field of formData){
      const[key, value] = field;
    }
<script/>

<form on:submit|preventDefault = {addVendorTest}>
<input
class="form-control"
type="text"
name="name"
value= "Test Name"
required
/>
<input
class="form-control"
type="text"
name="phone"
value= ""
required
/>
<input
class="form-control"
type="text"
 name="email"
value= ""
required
/>
                

This clears the input field value:这将清除输入字段值:

const inputElement = document.querySelector(".form-control[name='name']")
inputElement.value = ""

Either do this for each input field separately.分别为每个输入字段执行此操作。 Or better, create a loop that goes through all input fields in your form.或者更好的是,创建一个遍历表单中所有输入字段的循环。

https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#value https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#value

As an alternative to DOM querying, you could bind: the values to individual variables, an array or an object.作为 DOM 查询的替代方法,您可以将值bind:到单个变量、数组或 object。 Then you can reset the state and the form will also reset accordingly.然后您可以重置 state,表格也会相应地重置。

Eg using a dictionary object:例如使用字典 object:

<script>
    const valueDefaults = {
        name: "Test Name",
        phone: '',
        email: '',
    };
    let values = { ...valueDefaults };
    
    const addVendorTest = async (event) => {
        const formData = new FormData(event.target)
    
        // [Use formData]
        console.log([...formData]);

        values = { ...valueDefaults };
    }
</script>

<form on:submit|preventDefault = {addVendorTest}>
    <input class="form-control" type="text" name="name"
           bind:value={values.name} required />
    <input class="form-control" type="text" name="phone"
           bind:value={values.phone} required />
    <input class="form-control" type="text" name="email"
           bind:value={values.email} required />
    <button>Submit</button>
</form>

REPL REPL

Forms have a native reset mechanism which can be triggered via the reset() function or a button with type="reset" . Forms 具有本机重置机制,可以通过reset() function 或type="reset"按钮触发。

One limitation is that a value set via regular Svelte properties will not be considered the default.一个限制是通过常规 Svelte 属性设置的值不会被视为默认值。 To work around this, you can use setAttribute to set the default value.要解决此问题,您可以使用setAttribute设置默认值。 Eg例如

<script>
    const value = (node, param) => node.setAttribute('value', param);
    const addVendorTest = async (event) => {
        const formData = new FormData(event.target)
    
        // [Use formData]
        console.log([...formData]);

        event.target.reset();
    }
</script>

<form on:submit|preventDefault={addVendorTest}>
    <input class="form-control" type="text" name="name"
           use:value={'Test Name'} required />
    <input class="form-control" type="text" name="phone"
           required />
    <input class="form-control" type="text" name="email"
           required />
    <button>Submit</button>
</form>

REPL REPL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM