提交模态表单并将字段以JSON格式获取。

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

Submit modal form and get fields as JSON

问题

我有一个简单的模态框(div),用于在HTML中提交表单。

<div class="modal-body">
    <form id="myform">
        <div class="form-group">
            <label for="username">Username</label>
            <input type="text" placeholder="Username" class="form-control">
        </div>
        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" placeholder="Password" class="form-control">
        </div>
    </form>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-sm btn-outline-secondary" data-bs-dismiss="modal">Close</button>
    <button type="button" class="btn btn-sm btn-outline-secondary" onclick="addToFirebase()">Save changes</button>
</div>

当我尝试以JSON格式获取表单字段数据时,我得到了空数组[]。我需要将所有字段值以JSON格式获取。

function addToFirebase(){
    alert('Adding to firebase');
    var formData = JSON.stringify($("#myform").serializeArray());
    console.log(formData);
}
英文:

i have simple modal div to submit form in html

			&lt;div class=&quot;modal-body&quot;&gt;
                  &lt;form id=&quot;myform&quot;&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
                      &lt;label for=&quot;username&quot;&gt;Username&lt;/label&gt;
                      &lt;input type=&quot;text&quot; placeholder=&quot;Username&quot; class=&quot;form-control&quot;&gt;
                    &lt;/div&gt;
                    &lt;div class=&quot;form-group&quot;&gt;
                      &lt;label for=&quot;password&quot;&gt;Password&lt;/label&gt;
                      &lt;input type=&quot;password&quot; placeholder=&quot;Password&quot; class=&quot;form-control&quot;&gt;
                    &lt;/div&gt;
                  &lt;/form&gt;
                &lt;/div&gt;
                &lt;div class=&quot;modal-footer&quot;&gt;
                  &lt;button type=&quot;button&quot; class=&quot;btn btn-sm btn-outline-secondary&quot; data-bs-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
                  &lt;button type=&quot;button&quot; class=&quot;btn btn-sm btn-outline-secondary&quot; onclick=&quot;addToFirebase()&quot;&gt;Save changes&lt;/button&gt;
                &lt;/div&gt;

when i am trying to get the fields data in form of json i am getting blank array [] . I need to get all field values as json .

			 function addToFirebase(){
			  alert(&#39;Adding to firebase&#39;);
			  var formData = JSON.stringify($(&quot;#myform&quot;).serializeArray());
			  console.log(formData);
			}

答案1

得分: 0

表单元素缺少 name 属性。例如:

<input name="username" type="text" placeholder="用户名" class="form-control">

在序列化表单时,name 用作键/值对中的键。没有 name,就没有可分配值的键。

此外... 为了正确关联 <label> 元素,表单输入还需要一个与 for 属性匹配的 id

<input name="username" id="username" type="text" placeholder="用户名" class="form-control">
英文:

The form elements are missing name attributes. For example:

&lt;input name=&quot;username&quot; type=&quot;text&quot; placeholder=&quot;Username&quot; class=&quot;form-control&quot;&gt;

The name is used as the key in key/value pairs when serializing a form. Without one, there's no key to which the value can be assigned.


As an aside... For the &lt;label&gt; elements to be properly associated, the form inputs also need an id to match the for attributes:

&lt;input name=&quot;username&quot; id=&quot;username&quot; type=&quot;text&quot; placeholder=&quot;Username&quot; class=&quot;form-control&quot;&gt;

huangapple
  • 本文由 发表于 2023年5月25日 03:13:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326731.html
匿名

发表评论

匿名网友

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

确定