【发布时间】:2013-11-12 19:45:06
【问题描述】:
代码:
function disableOption(pos)
{
//document.getElementById("selectSuggestion").options[pos].disabled=true; <-- this is what I want to do
var option = $(#selectRating).children()[pos]; <-- doesnt work
$(#selectRating).find(option).prop("disabled", true);
}
function addEventListeners()
{
$(#selectRating).bind("focus", disableOption(0));
}
function init()
{
addEventListeners();
}
$(document).ready(init);
我对 jQuery API 和语法不太熟悉,我检查了 http://api.jquery.com/category/traversing/ 和其他类似的线程,但没有找到解决方案。
编辑:
固定代码:
function disableOption(pos)
{
$("#selectRating option:eq(" + pos + ")").prop("disabled", true);
}
function addEventListeners()
{
$("#selectRating").on("focus", function() {disableOption(0);});
}
function init()
{
addEventListeners();
}
$(document).ready(init);
【问题讨论】:
-
@Ryuji 您不需要在 disableoptions 中再次创建对象,请参阅我回答中的最后一个 sn-p。可以参考
this -
请说明您对这个问题投反对票的理由。出现了一些错误(因为我没有检查选择器周围的引号),但除此之外,这个问题的问题和解决方案仍然相关。
-
@PSL 你能给我一个指向 .call() 函数文档的链接吗?我在 jQuery API 页面上找不到任何东西:api.jquery.com/?s=call
-
@Ryuji 并非如此,这完全取决于您的设计方式。这里只是因为您调用函数而不是绑定处理程序,请参阅我的答案中的解释。如果您知道它始终是第一选择,那么您就可以这样做。
function disableOption() { $(this).children(":eq(0)").prop("disabled", true); } $("#selectRating").bind("focus", disableOption);
标签: javascript jquery properties tree-traversal