Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/402.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 如何使用jquery从表单元格中获取选项选择_Javascript_Jquery - Fatal编程技术网

Javascript 如何使用jquery从表单元格中获取选项选择

Javascript 如何使用jquery从表单元格中获取选项选择,javascript,jquery,Javascript,Jquery,我有一个表头单元格,其内容如下: <th rowspan="1" colspan="1"> <select> <option value="date" selected="selected">Date</option> <option value="number">Number</option> </select> </th> 但当我尝试读取所选值时,

我有一个表头单元格,其内容如下:

<th rowspan="1" colspan="1">
    <select>
        <option value="date" selected="selected">Date</option>
        <option value="number">Number</option>
    </select>
</th>
但当我尝试读取所选值时,会出现错误

TypeError: cell.selectedIndex is undefined
如果我像上面那样使用cell.toSource()将单元格转储到控制台,它看起来是这样的

var $selectors = $("#myTable thead tr th").each(function(index)
{
    var cell = $(this).html();
    console.log ("Cell:" + cell.toSource());
    var value = cell.options[cell.selectedIndex].value;
});
Cell:(new String("<select><option value=\"\"></option><option value=\"date\" selected=\"selected\">Date</option><option value=\"number\">Number</option></select>"))
单元格:(新字符串(“日期号”))

我不知道“新字符串”是从哪里来的,也不知道它是否是我问题的根源,或者如何进入它。如何获取所选值?

您可以像使用
jquery
一样,直接使用
.val
获取
所选值

$(function () {
  $('#myTable thead tr th').each(function () {
      alert($(this).find("select").val());
   });
});
.html()
返回一个字符串。您可以使用
$(“:selected”,this)
来获取
中选定的
选项
元素

var$selectors=$(“#myTable thead trth”)
.每个(功能(索引){
所选变量=$(“:selected”,此);
console.log(selected.val());
});

日期
数

使用
find
方法查找
选择
,然后使用
val
方法获取所选值

试试这个

 <table id="myTable"><thead>
      <tr>
        <th rowspan="1" colspan="1">
         <select onchange="change()">
            <option value="date" selected="selected">Date</option>
            <option value="number">Number</option>
         </select>
       </th>
      </tr>
    </thead>
</table>
$(文档).ready(函数(){
$(“#myTable thead tr th”)。每个(函数()
{
var value=$(this.find('select').val();//这将获得下拉列表的选定值
警报(值);
});
});

日期
数
试试这个:

<table>
    <thead>
        <tr>
            <th rowspan="1" colspan="1">
                <select>
                    <option value="date" selected="selected">Date</option>
                    <option value="number">Number</option>
                </select>
            </th>
        </tr>
    </thead>
</table/>

$('#dataTable thead tr th select [selected="selected"]').val();

日期
数
$(“#数据表thead tr th select[selected=“selected”]”)。val();
试试这个

 <table id="myTable"><thead>
      <tr>
        <th rowspan="1" colspan="1">
         <select onchange="change()">
            <option value="date" selected="selected">Date</option>
            <option value="number">Number</option>
         </select>
       </th>
      </tr>
    </thead>
</table>
这是为你工作的理由