Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用计数器更改select数组名称_Javascript_Jquery - Fatal编程技术网

Javascript 使用计数器更改select数组名称

Javascript 使用计数器更改select数组名称,javascript,jquery,Javascript,Jquery,我试图更改3个选择框的名称,输入名称要正确更改,但我正在使用选择框将头发拔出。如果在将文本字段name chages克隆到name=“dose_value[a1]”后选中fiddle,则每次克隆时都会添加一个数字。我希望选择框也发生同样的情况。有人能帮我吗 Html: 剂量值 剂量 G 镁 &穆;G 毫升 滴 安培 喘息 频率 每日的 屋宇署 Tds Qid 4小时 诺克特 隔日 周报 路线 人事军官 公共关系 销售确认书 感应电动机 四, 眼睛 内布斯 添加另一种药物 Javascript

我试图更改3个选择框的名称,输入名称要正确更改,但我正在使用选择框将头发拔出。如果在将文本字段name chages克隆到name=“dose_value[a1]”后选中fiddle,则每次克隆时都会添加一个数字。我希望选择框也发生同样的情况。有人能帮我吗

Html:


剂量值
剂量
G
镁
&穆;G
毫升
滴
安培
喘息
频率
每日的
屋宇署
Tds
Qid
4小时
诺克特
隔日
周报
路线
人事军官
公共关系
销售确认书
感应电动机
四,
眼睛
内布斯
添加另一种药物
Javascript:

     $(document).ready(function () {
//This line clones the row inside the '.row' class and transforms it to plain html.
var clonedRow = $('.row2').clone().html();
//This line wraps the clonedRow and wraps it <tr> tags since cloning ignores those tags
var appendRow = '<tr class = "row2">' + clonedRow + '</tr>';
var counter = 0;
$('#btnAddMore').click(function () {
//this line get's the last row and appends the appendRow when it finds the correct row.
$('.employmentHistoryForm tr:last').after(appendRow);
counter++;
$('.employmentHistoryForm tr:last input[type="text"]').attr('name', 'dose_value[' + 'a' + counter + ']');
$('.employmentHistoryForm tr:last select').attr('name', 'dose[' + 'a' + counter + ']');
$('.employmentHistoryForm tr:last select').attr('name', 'frequency[' + 'a' + counter + ']');
$('.employmentHistoryForm tr:last select').attr('name', 'route[' + 'a' + counter + ']');
$('.employmentHistoryForm tr:last').find('input').val('');
});
//when you click on the button called "delete", the function inside will be triggered.
$('.deleteThisRow').live('click', function () {
var rowLength = $('.row2').length;
//this line makes sure that we don't ever run out of rows.
if (rowLength > 1) {
deleteRow(this);
} else {
$('.employmentHistoryForm tr:last').after(appendRow);
deleteRow(this);
}
});
function deleteRow(currentNode) {
$(currentNode).parent().parent().remove();
}
});
$(文档).ready(函数(){
//此行克隆“.row”类中的行并将其转换为纯html。
var clonedRow=$('.row2').clone().html();
//这一行包装clonedRow并包装它的标记,因为克隆会忽略这些标记
var appendRow=''+clonedRow+'';
var计数器=0;
$('#btnAddMore')。单击(函数(){
//此行是最后一行,在找到正确的行时追加appendRow。
$('.employmentHistoryForm tr:last')。在(附录行)之后;
计数器++;
$('.employmentHistoryForm tr:last input[type=“text”]).attr('name','dose_value['+'a'+counter+']);
$('.employmentHistoryForm tr:last select').attr('name','dose['+'a'+counter+']);
$('.employmentHistoryForm tr:last select').attr('name','frequency['+'a'+counter+']);
$('.employmentHistoryForm tr:last select').attr('name','route['+'a'+counter+']);
$('.employmentHistoryForm tr:last').find('input').val('');
});
//当你点击“删除”按钮时,里面的功能就会被触发。
$('.deleteThisRow').live('click',函数(){
变量rowLength=$('.row2').length;
//这一行确保我们永远不会用完行。
如果(行长>1){
删除(本);
}否则{
$('.employmentHistoryForm tr:last')。在(附录行)之后;
删除(本);
}
});
函数deleteRow(currentNode){
$(currentNode).parent().parent().remove();
}
});

我将代码添加到fiddle:

尝试以下操作。希望这有助于

$('.employmentHistoryForm tr:last select option')。每个(
函数(){
this.value=this.value+'[a'+计数器+'];
}
)

您好,谢谢您的帮助。您提供的代码不起作用,它不会改变任何东西。
     $(document).ready(function () {
//This line clones the row inside the '.row' class and transforms it to plain html.
var clonedRow = $('.row2').clone().html();
//This line wraps the clonedRow and wraps it <tr> tags since cloning ignores those tags
var appendRow = '<tr class = "row2">' + clonedRow + '</tr>';
var counter = 0;
$('#btnAddMore').click(function () {
//this line get's the last row and appends the appendRow when it finds the correct row.
$('.employmentHistoryForm tr:last').after(appendRow);
counter++;
$('.employmentHistoryForm tr:last input[type="text"]').attr('name', 'dose_value[' + 'a' + counter + ']');
$('.employmentHistoryForm tr:last select').attr('name', 'dose[' + 'a' + counter + ']');
$('.employmentHistoryForm tr:last select').attr('name', 'frequency[' + 'a' + counter + ']');
$('.employmentHistoryForm tr:last select').attr('name', 'route[' + 'a' + counter + ']');
$('.employmentHistoryForm tr:last').find('input').val('');
});
//when you click on the button called "delete", the function inside will be triggered.
$('.deleteThisRow').live('click', function () {
var rowLength = $('.row2').length;
//this line makes sure that we don't ever run out of rows.
if (rowLength > 1) {
deleteRow(this);
} else {
$('.employmentHistoryForm tr:last').after(appendRow);
deleteRow(this);
}
});
function deleteRow(currentNode) {
$(currentNode).parent().parent().remove();
}
});