如何在javascript中获取克隆输入值
大家好,这里我有一个按钮,当我点击如何在javascript中获取克隆输入值,javascript,php,jquery,json,Javascript,Php,Jquery,Json,大家好,这里我有一个按钮,当我点击addelement按钮时,两个输入字段将一个接一个地出现,因为我使用了clone()函数。这里我的问题是在给每个输入文件指定了一些值后,单击submit按钮,我只得到了一个字段值。 这是我的密码 <html> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <script> $(document).ready(func
addelement
按钮时,两个输入字段将一个接一个地出现,因为我使用了clone()
函数。这里我的问题是在给每个输入文件指定了一些值后,单击submit
按钮,我只得到了一个字段值。
这是我的密码
<html>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script>
$(document).ready(function(){
$('#btn1').click(function(){
var $div = $('div[id^="trace-div"]:last');
var num = parseInt( $div.prop("id").match(/\d+/g), 10 ) +1;
var $klon = $div.clone().prop('id', 'trace-div'+num );
$klon.find("input[name='t1_x_axis']").attr("id", "x_axis_t"+num).val("");
$klon.find("input[name='t1_y_axis']").attr("id", "y_axis_t"+num).val("");
$div.after( $klon);
});
});
function chartsubmitbtn(){
var clones = $("input:text").clone();
//var str = JSON.stringify(clones);
console.log(clones.val());
return true;
}
</script>
<body>
<button type = "button" id = "btn1" > Add element</button>
<div id = "trace-div1">
<h4><b>Trace 1 </b></h4>
<form>
<table>
<tbody>
<tr>
<td><label>X Axis: </label></td>
<td><input type="text" name="t1_x_axis" id="x_axis_t1" size="50"></td>
</tr>
<tr>
<td><label>Y Axis: </label></td>
<td><input type="text" name="t1_y_axis" id="y_axis_t1" size="50"></td>
</tr>
</tbody>
</table>
</form>
</div>
<button type="button" id="chart-report-submit" onclick="chartsubmitbtn();">Submit</button>
</body>
</html>
$(文档).ready(函数(){
$('#btn1')。单击(函数(){
var$div=$('div[id^=“trace div”]:last');
var num=parseInt($div.prop(“id”).match(/\d+/g),10)+1;
var$klon=$div.clone().prop('id','trace div'+num);
$klon.find(“输入[name='t1\u x\u axis']”).attr(“id”,“x\u axis\u t”+num).val(“”);
$klon.find(“输入[name='t1\u y\u axis']”).attr(“id”,“y\u axis\u t”+num).val(“”);
$div.after($klon);
});
});
函数chartsubmitbtn(){
var clone=$(“输入:文本”).clone();
//var str=JSON.stringify(克隆);
console.log(clones.val());
返回true;
}
添加元素
踪迹1
X轴:
Y轴:
提交
注意:如果我没有在console.log()中使用val()
,它将提供json
数据。如何从中获取我的值。
谢谢,我无法理解您的确切需求,但是在您使用的克隆变量中,您得到了一个HTMLCollection对象数组。因此,您可以使用
[…clones]
将其转换为普通数组,然后在控制台中迭代以打印所有值
function chartsubmitbtn() {
var clones = $("input:text").clone();
[...clones].forEach(item => console.log(item.value));
return true;
}
希望这有帮助:)PHP会用相同的名称覆盖多个表单参数,除非您使用方括号中的“特殊语法”来创建数组,请参阅复制以获取详细信息。感谢您的回复,这里我需要chartsubmitbtn()中的这些输入值当我点击提交按钮时。因为我想使用ajax调用将这些值数据发送到控制器(codeigniter)。谢谢。我不知道该如何处理这些信息。Hi@Prakash感谢您的回复。您的代码为我工作,但最终我得到了这个http://,在这个单词前后三个空格,为什么会来?嗨,sandeep。你能再解释一下你想要如何在chartsubmitbtn中使用数据吗?在这种情况下,我们可以有许多输入字段。HI@Prakash,我从你的代码中获得了ans谢谢没有问题,我很乐意帮助:)