[英]I want to store input value in an array using jquery
i have table and inside td i have a input field,i want to to store all the id values in to array using jquery. 我有表和内部td我有一个输入字段,我想使用jquery将所有id值存储到数组中。
<td class="center">
<div id="divId">
<img src="<?php echo base_url()?>assets/backend/img/small.gif" width='40px' height='30px'></img>
</div>
<input type="hidden" class="box" id="boxId" name="foo" value="<?php echo $device->deviceId;?>" />
</td>
my javascript method is 我的javascript方法是
<script>
function yourfunction() {
var no=document.getElementById("boxId").value;
alert("no"+no);
}
var x=setInterval(yourfunction,600);
</script>
i tried using an loop but it didnt work always added up the first value. 我尝试使用循环,但它没有工作总是添加第一个值。
You have to use class proprety because Id is unique,This is a jquery solution: 你必须使用class proprety,因为Id是唯一的,这是一个jquery解决方案:
<td class="center"><div id="divId"><img src="<?php echo base_url()?>assets/backend/img/small.gif" width='40px' height='30px'></img> </div><input type="hidden" class="box" id="boxId" name="foo" value="<?php echo $device->deviceId;?>" /></td>
$(document).ready(function(){
function yourfunction(){
/*get all element having class "box"*/
$( ".box" ).each(function() {
alert($(this).val()); /*alert current element value*/
});
}
var x=setInterval(yourfunction,600);
});
With Javascript: 使用Javascript:
function yourfunction(){
var elements=document.getElementsByClassName("box");
for (var i = 0; i < elements.length; ++i) {
alert(elements[i].value);
}
}
var x=setInterval(yourfunction,600);
Assuming all the input elements to be processed are available on document ready:- 假设所有要处理的输入元素在文档就绪时可用: -
function extractBoxIds() {
var idList = [];
$('.box').each(function(index, element) {
idList.push($(element).val());
});
return idList;
}
$(document).ready(function() {
console.log(extractBoxIds());
});
If not, run extractBoxIds()
whenever all elements are available - after a timeout maybe. 如果没有,只要所有元素都可用,就运行
extractBoxIds()
- 超时后可能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.