我正试图将第一个设置为选中
我有
<select name="id[21]" class="atributosSort">
<option value="107">1. Sin adaptadores de video (+$45)</option>
<option value="108">2. Mini Displayport to DVI (+$112)</option>
<option value="109">3. Mini Displayport to VGA</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我试过了
jQuery(".atributosSort")[0].selectedIndex = 0;
Run Code Online (Sandbox Code Playgroud)
和
jQuery(".atributosSort option:first").attr('selected','selected');
Run Code Online (Sandbox Code Playgroud)
在如何使用jQuery选择<select>的第一个选项中进行了讨论 ? 但是当我检查DOM时,没有任何属性被添加到任何DOM
我正在使用jQuery(),因为我处于noConflict模式.这可能是问题吗?
当我运行两次尝试获取所选值时,我在脚本控制台中没有收到任何错误
另一件可能相关的事情就是我正在用这个方法对选项进行排序
function sortDropDownListByText() {
// Loop for each select element on the page.
jQuery("select").each(function() {
// Keep track of the selected option.
var selectedValue = jQuery(this).val();
// Sort all the options by text. I could easily sort these by val.
jQuery(this).html(jQuery("option", jQuery(this)).sort(function(a, b) {
return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
}));
// Select one option.
//jQuery(this).val(selectedValue);
});
}
// Use jQuery via jQuery(...)
jQuery(document).ready(sortDropDownListByText);
Run Code Online (Sandbox Code Playgroud)
我评论了jQuery(this).val(selectedValue); 因为我认为那条线正在设置一些选项,然后我不能覆盖它,但这没有任何区别.
有任何想法吗?谢谢
mat*_*ven 24
你有没有尝试过:
jQuery(".atributosSort option:first-child").attr("selected", true);
Run Code Online (Sandbox Code Playgroud)
?
| 归档时间: |
|
| 查看次数: |
48393 次 |
| 最近记录: |