gpt4 book ai didi

javascript - php 的动态名称属性

转载 作者:行者123 更新时间:2023-12-03 06:34:09 24 4
gpt4 key购买 nike

我使用此代码根据用户使用加号按钮添加的每个输入标签的索引键在 HTML 页面中生成动态名称属性:

var afterRemove = function afterRemove() {
var setIndex = function setIndex(inputNamePrefix) {
$('input[name^="' + inputNamePrefix + '"]').each(function (index, value) {
$(this).attr('name', '' + inputNamePrefix + index);
});
};
['jobTitle', 'organName', 'jobYearFrom', 'jobYearTo'].forEach(function (prefix) {
setIndex(prefix);
});
['research', 'researchComment'].forEach(function (prefix) {
setIndex(prefix);
});
['teachingsub', 'teachingpr', 'teachingplace'].forEach(function (prefix) {
setIndex(prefix);
});
['teacherMobile', 'teacherTel', 'teacherEmail'].forEach(function (prefix) {
setIndex(prefix);
});
['fieldofstudy', 'univercity', 'eduYearFrom', 'eduYearTo', 'eduPaper'].forEach(function (prefix) {
setIndex(prefix);
});

};

现在我需要在 PHP 中动态获取每个名称属性。例如,用户可能有 3 phoneNumber: {teacherMobile0,teacherMobile1,teacherMobile2} 。对于每个主题,我都需要一个 PHP 变量。我如何知道用户已生成了多少个名称?

最佳答案

好吧,我假设您有一个输入表,您可以让用户通过单击一个按钮来向该表添加行,该按钮运行一些 JavaScript 来添加新行

如果您将字段命名为

<td><input name="fieldofstudy[]" .....></td>
<td><input name="univercity[]" .....></td>
etc etc

您不必再担心向这些输入添加前缀或后缀计数。

那么在 javascript 中创建新行所需要做的就是 clone表格中的最后一行,将每个单元格的 .val() 清空,新行已准备就绪

然后在 PHP 中,您将收到每个变量的数组,即

$_POST['fieldofstudy'][]
$_POST['univercity'][]

您可以将其处理为

foreach ( $_POST['fieldofstudy'] as $idx => $fieldofstudy) {
echo $fieldofstudy;
echo $_POST['univercity'][$idx];
}

关于javascript - php 的动态名称属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38308228/

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