gpt4 book ai didi

javascript - 获取每个表单部分的 id 和值

转载 作者:行者123 更新时间:2023-12-03 00:04:23 25 4
gpt4 key购买 nike

我有下面的表单,并尝试获取 id 和值,并将数据放入 json 中以传递给我的 Controller 。下面是代码。我看不出我错过了什么。这是第一次进行网络开发

<div class="form-section">
<div class="section-header">
<span class="collapse-symbol"><i class="fas fa-angle-right"></i></span>
<span class="section-title">
Term
</span>

</div>
<div class="content" id="quote-edit-form">

<div class="row-container">

<div class="row-child">
<label class="control-label" for="StartDate">StartDate Date</label>

<input class="form-control text-box single-line" data-val="true" data-val-date="The field Start Date must be a date." id="Start-date" name="Terms.StartDate" type="datetime" value="" />

<label class="control-label" for="Terms_Date">Terms Date</label>

<input class="form-control text-box single-line" data-val="true" data-val-date="The field Terms Date must be a date." id="continuity-date" name="Terms.ContinuityDate" type="datetime" value="" />

<label class="control-label" for="Terms Period">Extension Period</label>

<input class="form-control text-box single-line" id="expiry-date" name="Terms.ExtensionPeriod" type="text" value="" />
</div>

<div class="row-child">
<label class="control-label" for="Terms.Choice">Choice</label>

<input class="form-control text-box single-line" id="expiry-date" name="Terms.Law" type="text" value="" />

<label class="control-label" for="Terms_C">C</label>

<input class="form-control text-box single-line" data-val="true" data-val-number="The field Commission must be a number." id="expiry-date" name="Terms.C" type="text" value="" />

<label class="control-label" for="Terms_P">P</label>

<input class="form-control text-box single-line" id="expiry-date" name="Terms.P" type="text" value="" />


</div>
</div>

我尝试了以下方法从我的 html 中捕获数据

 var items = {};
$('.form-control text-box single-line').each(function () {
var obj = {
id: $(this).find('.id').val(),
value: $(this).find('.value').val()
};
o.items.push(obj);
});

最佳答案

这里将使用 id/value 构建一个对象。注意:我添加了设置间隔,以便您可以键入并查看正在填充的对象。

const buildItems = () => {
let items = {};
document.querySelectorAll('input').forEach(element => {
items[element.id] = element.value;
});
return items;
};

// check every 5secs (just to demo working)
setInterval(() => console.log(buildItems()), 5000);
<div class="form-section">
<div class="section-header">
<span class="collapse-symbol"><i class="fas fa-angle-right"></i></span>
<span class="section-title">
Term
</span>

</div>
<div class="content" id="quote-edit-form">

<div class="row-container">

<div class="row-child">
<label class="control-label" for="StartDate">StartDate Date</label>

<input class="form-control text-box single-line" data-val="true" data-val-date="The field Start Date must be a date." id="Start-date" name="Terms.StartDate" type="datetime" value="" />

<label class="control-label" for="Terms_Date">Terms Date</label>

<input class="form-control text-box single-line" data-val="true" data-val-date="The field Terms Date must be a date." id="continuity-date" name="Terms.ContinuityDate" type="datetime" value="" />

<label class="control-label" for="Terms Period">Extension Period</label>

<input class="form-control text-box single-line" id="expiry-date" name="Terms.ExtensionPeriod" type="text" value="" />
</div>

<div class="row-child">
<label class="control-label" for="Terms.Choice">Choice</label>

<input class="form-control text-box single-line" id="expiry-date" name="Terms.Law" type="text" value="" />

<label class="control-label" for="Terms_C">C</label>

<input class="form-control text-box single-line" data-val="true" data-val-number="The field Commission must be a number." id="expiry-date" name="Terms.C" type="text" value="" />

<label class="control-label" for="Terms_P">P</label>

<input class="form-control text-box single-line" id="expiry-date" name="Terms.P" type="text" value="" />


</div>
</div>

关于javascript - 获取每个表单部分的 id 和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55031830/

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