我有表,并且在 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>
我的javascript方法是
<script>
function yourfunction() {
var no=document.getElementById("boxId").value;
alert("no"+no);
}
var x=setInterval(yourfunction,600);
</script>
我尝试使用循环,但它不起作用,总是将第一个值相加。
最佳答案
你必须使用类属性,因为 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);
});
使用 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);
关于javascript - 我想使用 jquery 将输入值存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39632338/