[英]Why can't my javascript see which html option is selected
I've got an html select - 我有一个html select-
<select name="startDay" id="startDay">
<option value="01">Sun 1</option>
<option value="02">Mon 2</option>
<option value="03">Tue 3</option>
<option value="04">Wed 4</option>
<option value="05">Thu 5</option>
<option value="06">Fri 6</option>
<option value="07">Sat 7</option>
<option value="08" selected>Sun 8</option>
<option value="09">Mon 9</option>
<option value="10">Tue 10</option>
<option value="11">Wed 11</option>
<option value="12">Thu 12</option>
<option value="13">Fri 13</option>
<option value="14">Sat 14</option>
<option value="15">Sun 15</option>
<option value="16">Mon 16</option>
<option value="17">Tue 17</option>
<option value="18">Wed 18</option>
<option value="19">Thu 19</option>
<option value="20">Fri 20</option>
<option value="21">Sat 21</option>
<option value="22">Sun 22</option>
<option value="23">Mon 23</option>
<option value="24">Tue 24</option>
<option value="25">Wed 25</option>
<option value="26">Thu 26</option>
<option value="27">Fri 27</option>
<option value="28">Sat 28</option>
</select>
When I use this code on jsbin I can identify the selected option with jQuery - 当我在jsbin上使用此代码时,我可以使用jQuery识别选定的选项-
$('#startDay').val()
But on the site I'm working on, http://test.mapov.com/?d=london . 但是在我正在工作的网站上, http://test.mapov.com/?d=london 。 The same code fails - 相同的代码失败-
> $('#startDay').val()
> null
I can't get anything with pure js either - 我也无法用纯js获得任何东西-
> document.getElementById('startDay').options.selectedIndex
> -1
I've checked that there's no other element with that ID on the page. 我检查了页面上是否没有其他具有该ID的元素。 And I cut and paste the html into pasteBin ( http://jsbin.com/bivudinagu/1/edit?html,js,console ). 然后将html剪切并粘贴到pasteBin( http://jsbin.com/bivudinagu/1/edit?html,js,console )中。 I just can't make any sense of what's going on here. 我只是对这里发生的事情一无所知。
This is from my chrome console - 这是从我的Chrome控制台-
> document.getElementById('startDay').options[7]
> <option value="08" selected>Sun 8</option>
> document.getElementById('startDay').options[7].selected
> false
??!? ??!?
Initially, you have to set an id to your select
, 最初,您必须将一个ID设置为select
,
<select id="startDay">
Then you can get the value of the selected option as: 然后,您可以通过以下方式获得所选选项的值:
$('#startDay option:selected').val()
$(function(){ document.write($('#startDay option:selected').val()); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="startDay"> <option value="01">Sun 1</option> <option value="02">Mon 2</option> <option value="08" selected>Sun 8</option> </select>
Here in your question, and in your pastebin, you have 在您的问题和粘贴框中,您有
<option value="08" selected>8</option>
However, on your actual page on test.mapov.com you have 但是,在test.mapov.com的实际页面上,
<option selected="selected" value="08">8</option>
(viewing page source and inspecting the DOM element both showed this) (查看页面源代码和检查DOM元素都显示了这一点)
In my javascript console in Chrome on your actual site I get the following 在您实际网站上的Chrome浏览器的JavaScript控制台中,我得到以下信息
> $("#startDay").val();
null
> $("#startDay option:selected").val();
undefined
If I tamper with the DOM and change selected="selected"
into just selected
, then I get 如果我篡改DOM,并将selected="selected"
更改为just selected
,那么我得到
> $("#startDay").val();
"08"
> $("#startDay option:selected").val();
"08"
OK, I found the problem. 好的,我发现了问题。 Jquery allows you to set an invalid value with $('#el').val('non-existent-option')
, without throwing an error or updating the option elements to remove the existing selected
attribute. jQuery允许您使用$('#el').val('non-existent-option')
设置无效值,而不会引发错误或更新option元素以删除现有的selected
属性。 I was calling 我在打电话
$('#startDay').val(8)
Somewhere else in the code. 代码中的其他地方。 There wasn't an <option>
with this value (the correct value was a string '08'
). 没有使用该值的<option>
(正确的值是字符串'08'
)。 This wasn't giving off any error's and I couldn't see any evidence of it updating the dom - 这并没有释放任何错误,我看不到有任何证据证明它会更新dom-
> document.getElementById('startDay').options[7]
> <option value="08" selected>Sun 8</option>
It was however leaving $('#startDay').val()
returning null
. 但是,这使得$('#startDay').val()
返回null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.