简体   繁体   English

我想使用jquery将输入值存储在数组中

[英]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.

相关问题 我想将数组值存储在隐藏的输入字段中 - I want to store an array values in a hidden input field 我想在变量中存储(数组值的对象数组) - i want store (array- of object- of array value )in variable 在我的情况下,我想推送输入值并将其存储在本地存储 VUEX 的数组中。 我试过这个,但它不工作 - In my situation I want to push input value and store it in array in local storage VUEX. I've tried this but it's not working 使用jquery将输入值与数组值进行比较 - Comparing an input value to an array value using jquery 我想使用 jQuery 获取复选框的值 - I want to get the value of checkbox using jQuery 如何使用javascript或jquery在web本地storege中存储输入值? - How can I store an input value in web local storege using javascript or jquery? 使用jQuery设置输入数组值 - Set Input array value using jquery 使用 jquery 获取数组输入键和值 - Getting array input key and value using jquery 我想向数组添加一个值,尝试使用拼接,但它想要工作吗? - I want to add an value to an array, trying using splice, but it want work? 使用jQuery获取输入,然后搜索数组,然后如果我的值在数组中,则输出所需的答案 - Using jQuery to get an input then searching through an array, then if I value is in array, output the desired answers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM