[英]pass text box array values into javascript
I have a hidden text-box which is in for loop.(Codeigniter View File) 我在for循环中有一个隐藏的文本框。(Codeigniter查看文件)
<input type="hidden" value="<?php echo $name->file_name;?>" name="high_name[]" />
i want to pass this array values into my java script function. 我想将此数组值传递到我的Java脚本函数中。 I dont know how to do it for array. 我不知道如何为数组做。
$(".button").click( function() {
var name = document.getElementById('high_name[]').value;
alert(name);
});
what i want is, get these names and pass it to the another text-area's value. 我想要的是获取这些名称,并将其传递给另一个文本区域的值。
<textarea>.....</textarea>
Something like this? 像这样吗
NOTE : I adjusted the HTML slightly. 注意 :我稍微调整了HTML。 A class was added to the inputs and they were made text
in stead of hidden
. 在输入中添加了一个类,使它们成为text
,而不是hidden
text
。 The text
is for example purposes, see what is happening. text
仅供参考,请参阅发生的情况。 The class because (as far as i know) not all browsers select elements with a array notation all that good. 该类是因为(据我所知)并非所有浏览器都选择具有数组表示法的元素。
$(".button").click( function() { $('input.high_name').each(function(){ var currentVal = $("#toHere").val(); $("#toHere").val(currentVal + "\\n" + $(this).val()); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" value="Value1" name="high_name[]" class="high_name" /> <input type="text" value="Value2" name="high_name[]" class="high_name" /> <input type="text" value="Value3" name="high_name[]" class="high_name" /> <button class="button">Click me</button> <textarea id="toHere"></textarea>
You might want to optimize the \\n
part. 您可能需要优化\\n
部分。 But that comes when you implement this example to your own needs 但是,当您根据自己的需要实施此示例时
document.getElementById('high_name[]')
does not work since high_name[]
is not the Id of the HTML element. document.getElementById('high_name[]')
无效,因为high_name[]
不是HTML元素的ID。 Instead try this 而是试试这个
var name = $('input[name="high_name[]"]').val();
$("#textarea_id").html(name);
//ensure that jQuery is included //确保包含jQuery
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.