似乎我找不到正确的答案,因为我不知道怎么问这个问题.所以我会尽量描述一下情况。
我有HTML (智能模板):
{foreach $dns_records as $record}
<tr>
<td class="text-center align-middle">
<a href="javascript:void(0);" class="text-danger delete-record" title="Delete record"><i class="fas fa-times-circle"></i></a>
</td>
<td>
<input type="text" name="record[]" id="name" class="form-control" value="{$record.name|escape:html}" />
</td>
<td>
<select class="form-select" name="record[]" id="ttl" {if !$record.ttl}disabled{/if}>
<option value="">-</option>
{if $record.ttl}
{foreach from=$dns_allowed_ttl item=ttl key=key}
<option value="{$ttl}" {if $ttl eq $record.ttl}selected{/if}>{$ttl} min.</option>
{/foreach}
{/if}
</select>
</td>
<td>
<select class="form-select" name="record[]" id="type">
<option value="">-</option>
{foreach from=$dns_record_types item=type key=key}
<option value="{$type}" {if $type eq $record.type}selected{/if}>{$type}</option>
{/foreach}
</select>
</td>
<td style="width: 10%">
<input type="text" name="record[]" id="prio" class="form-control" value="{if $record.priority}{$record.priority|escape:html}{else}-{/if}" {if $record.type neq 'MX'}disabled{/if}/>
</td>
<td>
<input type="text" name="record[]" id="content" class="form-control" value="{$record.content|escape:html}"/>
</td>
</tr>
{foreachelse}
<tr>No records found.</tr>
{/foreach}我想向PHP提交这些输入字段,如下所示:
0: ['name', 'ttl', 'type', 'prio', 'content'],
1: ['name', 'ttl', 'type', 'prio', 'content'],
2: ['name', 'ttl', 'type', 'prio', 'content'],
<...>我通过jquery使用AJAX:
$("#records-table").submit(function(event) {
event.preventDefault();
if (request) {
request.abort();
}
var $inputs = $("#records-table").find("input, select");
var record = $('input[name="record[]"]').map(function(){
return this.value;
}).get();
var data = {
records: record,
zone_id: $inputs.find("#zone_id").val(),
action: $inputs.find("#action").val(),
csrf: $inputs.find("#csrf").val(),
}
console.log(data);
request = $.ajax({
cache: false,
url: 'ajax/dns_zone.php',
type: "post",
dataType: "json",
data: JSON.serialize(data)
});
request.done(function (response) {
if (response.response == '1') {
window.location = response.redirecturl;
}
});
});如何实现我的目标?我理解输入字段必须具有唯一的ID,但我通过[]的名称来选择它们。
发布于 2022-01-29 07:13:56
这里有一个解决我的问题的方法:
var final = [];
var value = [];
var i = 1;
$("input[name='record[]']").each(function() {
value.push($(this).val());
if(i % 5 == 0) {
final.push(value);
value = [];
}
i++;
});
var data = {
records: value,
zone_id: $(this).find("input[name='zone_id']").val(),
action: $(this).find("input[name='action']").val(),
csrf: $(this).find("#csrf").val(),
}
request = $.ajax({
cache: false,
url: 'ajax/dns_zone.php',
type: "post",
dataType: "json",
data: $(this).serialize(data)
});以下是HTML部分:
<form id="records-table">
{$csrf}
<input type="hidden" name="action" value="edit_records" />
<input type="hidden" name="zone_id" value="{$zone.id}" />
{counter assign=i start=1 print=false}
{foreach $dns_records as $record}
<tr>
<td class="text-center align-middle">
<a href="javascript:void(0);" class="text-danger delete-record" title="Delete record"><i class="fas fa-times-circle"></i></a>
</td>
<td>
<input type="text" name="record[{$i}][name]" id="name" class="form-control" value="{$record.name|escape:html}" />
</td>
<td>
<select class="form-select" name="record[{$i}][ttl]" id="ttl" {if !$record.ttl}disabled{/if}>
<option value="">-</option>
{if $record.ttl}
{foreach from=$dns_allowed_ttl item=ttl key=key}
<option value="{$ttl}" {if $ttl eq $record.ttl}selected{/if}>{$ttl} min.</option>
{/foreach}
{/if}
</select>
</td>
<td>
<select class="form-select" name="record[{$i}][type]" id="type">
<option value="">-</option>
{foreach from=$dns_record_types item=type key=key}
<option value="{$type}" {if $type eq $record.type}selected{/if}>{$type}</option>
{/foreach}
</select>
</td>
<td style="width: 10%">
<input type="text" name="record[{$i}][prio]" id="prio" class="form-control" value="{if $record.priority}{$record.priority|escape:html}{else}-{/if}" {if $record.type neq 'MX'}disabled{/if}/>
</td>
<td>
<input type="text" name="record[{$i}][content]" id="content" class="form-control" value="{$record.content|escape:html}"/>
</td>
</tr>
{counter}
{foreachelse}
<tr>No records found.</tr>
{/foreach}
</form>在柜台上写张便条。它迭代数组。这很重要!
发布于 2022-01-20 09:24:23
因此,所有输入的名称都是record[],这将使record[]值类似于:
['name', 'ttl', 'type', 'prio', 'content','name', 'ttl', 'type', 'prio', 'content','name', 'ttl', 'type', 'prio', 'content']然后你可以试试这个:
var final = [];
var value = [];
var i = 1;
$("input[name='record[]']").each(function() {
value.push($(this).val());
if(i % 5 == 0) {
final.push(value);
value = [];
}
i++;
});它将遍历所有record[],为每个分组输入创建新变量。它将是:
[["name", "ttl", "type", "prio", "content"], ["name", "ttl", "type", "prio", "content"], ["name", "ttl", "type", "prio", "content"]]https://stackoverflow.com/questions/70783116
复制相似问题