我有以下代码..。我简化了它,不将options包含在select标记中:
<div>
Show all tasks where assigned to <span><strong></strong>
<select></select></span>
and project is <span><strong></strong>
<select></select></span>
and status is <span><strong></strong>
<select></select></span>
and category is <span><strong></strong>
<select></select></span>
</div>当页面加载时,我想做以下工作:
select标签之间的每个select输入中显示所选项目的文本。select标签.当用户单击strong时,我希望它隐藏strong标记并显示select。
尽管如此,由于缺少更好的术语,如果我将特定的id值分配给每个过滤器,我就可以使用它了。我想简化我的jquery,这样我就可以避免额外的HTML。
希望这有点道理..。这就是我使用jquery的地方..。
$(function () {
$("#filter select").hide();
$("#filter strong").each(function () {
$(this).html($(this).siblings("select:first option:selected").text())); // not working
});
$("#filter strong").each(function () {
$(this).bind("click", function () {
$(this).css("display", "none"); // not sure if this will work, need to get the first part working
$(this).siblings("select:first").css("display", "");
});
});
});我将继续努力,但希望能提供任何帮助!我认为我的困难在于sibling选择器如何在jQuery中工作。
更新
谢谢亚历克斯和安迪..。下面是最后的工作解决方案:
$(function () {
$("#filter select").hide();
$("#filter strong").html(function () {
return $(this).next().find(":selected").text();
})
$("#filter strong").each(function () {
$(this).bind("click", function () {
$(this).css("display", "none");
$(this).siblings("select:first").css("display", "");
});
});
}); 发布于 2011-01-08 15:16:04
你可以用:
$('strong').click(function(){ $(this).hide().next().show(); });这里有一个太多的方括号-> .text();
$("#filter strong").each(function () {
$(this).html($(this).siblings("select:first option:selected").text())); // not working
});发布于 2011-01-08 15:08:18
我想我的斗争是如何在jQuery中使用同胞选择器。
siblings()方法将选择与当前元素相邻的所有元素。本质上和.
var element = $('#something'),
siblings = element.parent().children().not(element);您还可能试图在将选择器字符串传递给siblings()时选择太多。
试着用..。
.siblings("select:first").find("option:selected")https://stackoverflow.com/questions/4634477
复制相似问题