Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从html表格cel中的下拉列表中获取值_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如何从html表格cel中的下拉列表中获取值

Javascript 如何从html表格cel中的下拉列表中获取值,javascript,jquery,html,Javascript,Jquery,Html,我试图动态检索html表中下拉列表的值。这是我的html <tr> <td>ITMF-502</td> <td>PUMA</td> <td>1</td> <td>0</td> <td>HALF FRAME/ METAL/ NB/ LOW PRICE</td><td>KOTTAWA</td> <td> <sel

我试图动态检索html表中下拉列表的值。这是我的html

<tr>
 <td>ITMF-502</td>
 <td>PUMA</td>
 <td>1</td>
 <td>0</td>
 <td>HALF FRAME/ METAL/ NB/ LOW PRICE</td><td>KOTTAWA</td>
 <td>
  <select name="loc" id="location0" class="form-control">
   <option value="BR000002">KOTTAWA</option>
   <option value="BR000007">NEGOMBO</option>
  </select>
 </td>
</tr>

ITMF-502
彪马
1.
0
半框/金属/NB/低价Kottawa
科塔瓦
内贡博
这是我的java脚本

var table = document.getElementById('Items');
var tableRowCount = $("#Items > tbody > tr").length;
for (var i = 1; i <= tableRowCount; i++) {
    var obj = {
        itemCode: table.rows.item(i).cells[0].innerText,
        currentLocation: table.rows.item(i).cells[5].innerText,
        newLocation: table.rows.item(i).cells[6].find("select").val()
    };
    items.push(obj);
}
var table=document.getElementById('Items');
var tableRowCount=$(“#项>正文>正文”)。长度;
对于(var i=1;i请尝试下面的代码

HTML


ITMF-502
彪马
1.
0
半框/金属/NB/低价Kottawa
科塔瓦
内贡博
JavaScript:

var tableRowCount = document.getElementById('location0').length;;
for (var i = 0; i < tableRowCount; i++) {
    alert(document.getElementById('location0')[i].value);
}
var tableRowCount=document.getElementById('location0').length;;
对于(变量i=0;i
我认为基本问题是FOR忽略了第一行(索引为0的那一行)。此外,您还将添加到不存在的items数组中

这是改进的jQuery版本:

var items = [],
    table = $(document.getElementById('Items')),
    tableRows = table.find('tr'),
    obj,
    tableRow;

for (var i = 0; i < tableRows.length; i++) {
    tableRow = tableRows.eq(i);

    obj = {
        itemCode: tableRow.find('td').first().text(),
        currentLocation: tableRow.find('td').eq(5).text(),
        newLocation: tableRow.find("select").val()
    };
    items.push(obj);
}

console.log(items);
var items=[],
表=$(document.getElementById('Items'),
tableRows=table.find('tr'),
obj,
tableRow;
对于(var i=0;i
html中的#项在哪里?您不能对DOM对象调用jQuery方法。查看JavaScript错误控制台。它会告诉您哪里出了问题。然后您应该循环选择框数据。我需要获取项的值。这将显示一个文本如何将动态值设置为“location0”
var items = [],
    table = $(document.getElementById('Items')),
    tableRows = table.find('tr'),
    obj,
    tableRow;

for (var i = 0; i < tableRows.length; i++) {
    tableRow = tableRows.eq(i);

    obj = {
        itemCode: tableRow.find('td').first().text(),
        currentLocation: tableRow.find('td').eq(5).text(),
        newLocation: tableRow.find("select").val()
    };
    items.push(obj);
}

console.log(items);