简体   繁体   English

Internet Explorer的jQuery选择框问题

[英]Jquery select box problem with Internet Explorer

Ok, this might be a really odd problem. 好的,这可能是一个非常奇怪的问题。 I am writing a web application, and I want to change the value of a certain select box: 我正在编写一个Web应用程序,并且想要更改某个选择框的值:

<select name="type" class="type">
<option value="word">word</option>
<option value="digit">digit</option>
<option value="letter">letter</option>
<option value="single">single character</option>
<option value="space">space</option>
</select>

In my code I have this: 在我的代码中,我有:

switch ( target ) {
case "d":
$("select").val('digit');
break;
case "w":
$("select").val('word');
break;
case "s":
$("select").val('space');
break;
}

Here is the problem. 这是问题所在。 This code works as expected in all browsers...except for IE. 此代码可在所有浏览器中正常工作……除了IE。 Internet Explorer (8, if you're curious) processes them all correctly except for the $("select").val("digit") part. 除了$(“ select”)。val(“ digit”)部分以外,Internet Explorer(如果您有好奇,请使用8)正确处理它们。 It just won't change the value for some reason. 只是出于某种原因不会更改值。 The javascript parser properly navigates to that case in the switch and all, but it refuses to change the value of the select box for that one case. javascript解析器可以在所有开关中正确导航到该案例,但是它拒绝更改该案例的选择框的值。 Anyone have any ideas? 有人有想法么?

Have you tried 你有没有尝试过

$('select').find('option[value=digit]').prop('selected', true); // and etc.

instead? 代替?

edit well I would probably try that myself, because that's just me, but looking at the jQuery source it pretty much does that for you anyway. 编辑得好我可能会自己尝试,因为那只是我,但是看看jQuery源,无论如何它几乎都能为您做到。 (That is, jQuery doesn't just try to set the "value" attribute on the <select> tag, which I think works in FF but not IE.) (也就是说,jQuery不仅尝试在<select>标记上设置“ value”属性,我认为该属性在FF中有效,但不适用于IE。)

edit more well I set up a test page and it works fine for me in FF and also in IE8. 编辑得更好我设置了一个测试页,它对FF和IE8都适用。 You can look at my test page here: http://gutfullofbeer.net/select.html 您可以在这里查看我的测试页: http : //gutfullofbeer.net/select.html

edit — 12 Jul 2012 — .prop() is preferred now (jQuery 1.6), so I've updated the example. 编辑 — 2012年7月12日—现在首选.prop() (jQuery 1.6),所以我更新了示例。

You can try adding the word 'selected' to the option you want to show rather than changing the value of the select tag. 您可以尝试在要显示的选项中添加单词“ selected”,而不用更改select标签的值。 You would want the HTML of the option you want to look like... 您可能想要该选项的HTML外观...

<option selected value="word">word</option>

Where the word is in the tag does not matter. 单词在标签中的位置无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM