gpt4 book ai didi

javascript - 序列化表单并使用标签和段落作为数组中的键

转载 作者:可可西里 更新时间:2023-11-01 01:11:37 25 4
gpt4 key购买 nike

我有一个顶部的基本表单,只有一些输入字段。但再往下是一个列表,其中包含类别、问题以及每个问题使用开关(单选按钮)的三个可能答案。

我的 HTML 看起来像这样:

<form class="addwerkplekinspectie" action="#" method="post">
<div class="row">
<div class="col-md-3">
<p class="inputtitles">Werkplekinspectie template:</p>
</div>
<div class="col-md-9">
<p class="inputtitles nomarginleft">jjjj</p>
</div>
<div class="col-md-3">
<p class="inputtitles">Titel:</p>
</div>
<div class="col-md-9">
<input type="text" name="titel" class="form-control name_list">
</div>
<div class="col-md-3">
<p class="inputtitles">Inspectienummer:</p>
</div>
<div class="col-md-9">
<input type="text" name="inspectienummer" class="form-control name_list">
</div>
<div class="col-md-3">
<p class="inputtitles">Inspectiedatum:</p>
</div>
<div class="col-md-9">
<input type="date" name="datum" class="form-control name_list">
</div>
<div class="col-md-3">
<p class="inputtitles">Naam inspecteur:</p>
</div>
<div class="col-md-9">
<input type="text" name="naaminspecteur" class="form-control name_list">
</div>
<div class="col-md-3">
<p class="inputtitles">Locatie/Vestiging</p>
</div>
<div class="col-md-9">
<input type="text" name="locatie" class="form-control name_list">
</div>
<div class="col-md-3">
<p class="inputtitles">Naam/ Namen geauditeerden</p>
</div>
<div class="col-md-9">
<input type="text" name="naamgeauditeerden" class="form-control name_list">
</div>
</div>
<label class="categorytitle">jjjj</label>
<div class="row">
<div class="col-md-8">
<p class="questionclass">hhhh</p>
</div>
<div class="col-md-4">
<div class="container text-right">
<input type="radio" name="group1" id="radio-1" value="ok">
<label class="radiotoggle" for="radio-1"><span class="radio">Ok</span></label>
<input type="radio" name="group1" id="radio-2" value="fout">
<label class="radiotoggle" for="radio-2"><span class="radio">Fout</span></label>
<input type="radio" name="group1" id="radio-3" value="nvt">
<label class="radiotoggle" for="radio-3"><span class="radio">N.v.t</span></label>
</div>
</div>
<div class="col-md-8">
<p class="questionclass">jjj</p>
</div>
<div class="col-md-4">
<div class="container text-right">
<input type="radio" name="group4" id="radio-4" value="ok">
<label class="radiotoggle" for="radio-4"><span class="radio">Ok</span></label>
<input type="radio" name="group4" id="radio-5" value="fout">
<label class="radiotoggle" for="radio-5"><span class="radio">Fout</span></label>
<input type="radio" name="group4" id="radio-6" value="nvt">
<label class="radiotoggle" for="radio-6"><span class="radio">N.v.t</span></label>
</div>
</div>
</div>
<label class="categorytitle">Testt</label>
<div class="row">
<div class="col-md-8">
<p class="questionclass">test</p>
</div>
<div class="col-md-4">
<div class="container text-right">
<input type="radio" name="group7" id="radio-7" value="ok">
<label class="radiotoggle" for="radio-7"><span class="radio">Ok</span></label>
<input type="radio" name="group7" id="radio-8" value="fout">
<label class="radiotoggle" for="radio-8"><span class="radio">Fout</span></label>
<input type="radio" name="group7" id="radio-9" value="nvt">
<label class="radiotoggle" for="radio-9"><span class="radio">N.v.t</span></label>
</div>
</div>
</div>
</form>
<div class="lijstresult">
</div>
<button type="button" class="btn btn-secondary btn-lg waves-effect btnadd savewpi">Werkplekinspectie opslaan</button>

为了理解我的意思,这就是我的页面的样子:

enter image description here

当我序列化表单并将其发布到我的 php 脚本时,我打印的数组看起来像这样(除了单选按钮,我将所有内容留空):

Array
(
[0] => Array
(
[name] => titel
[value] =>
)

[1] => Array
(
[name] => inspectienummer
[value] =>
)

[2] => Array
(
[name] => datum
[value] =>
)

[3] => Array
(
[name] => naaminspecteur
[value] =>
)

[4] => Array
(
[name] => locatie
[value] =>
)

[5] => Array
(
[name] => naamgeauditeerden
[value] =>
)

[6] => Array
(
[name] => group1
[value] => nvt
)

[7] => Array
(
[name] => group4
[value] => fout
)

[8] => Array
(
[name] => group7
[value] => nvt
)

)

如您所见,我无法判断哪个答案属于哪个问题以及哪个问题属于哪个类别。

如何使用表格(问题)中的标签(类别)和段落?

理想情况下我的数组看起来像这样:

Array
(
[information] => Array
(
[titel] => inserted value
[inspectienummer] => inserted value
[datum] => inserted value
[naaminspecteur] => inserted value
[locatie] => inserted value
[naamgeauditeerden] => inserted value
)

[questionlist] => Array
(
[jjjj] => Array
(
[hhhh] => ok
[jjj] => fout
)
[testt] => Array
(
[test] => n.v.t.
)
)
)

这就是我目前序列化我的表单的方式:

$( ".savewpi" ).on( "click", function( event ) {
event.preventDefault();

url = 'includes/savewpi.php';
$wpi = $( '.addwerkplekinspectie' ).serializeArray();

// post the data
var posting = $.post(url, {
wpi: $wpi
});

console.log($wpi);

posting.done(function( data ) {
$( ".lijstresult" ).empty().slideDown('fast').append( data );
});
});

最佳答案

您可以使用 input 字段的 name 属性来构造您发布的数据。像这样创建您的输入字段。

<input type="date" name="information[titel]" class="form-control name_list">
<input type="date" name="information[inspectienummer]" class="form-control name_list">
<input type="date" name="information[naaminspecteur]" class="form-control name_list">
<input type="date" name="information[locatie]" class="form-control name_list">
<input type="date" name="information[naamgeauditeerden]" class="form-control name_list">

<input type="radio" name="questionlist[jjjj][hhhh]" id="radio-1" value="ok">
<input type="radio" name="questionlist[jjjj][jjj]" id="radio-2" value="fout">

关于javascript - 序列化表单并使用标签和段落作为数组中的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54276848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com