查找最接近的类
find closest class
我想找到给定对象的最接近的类。我的标记如下:
<div class="table_settings">
<div style="float:left;">
<div class="stats_options" id="yearSelector" style="width:140px;">
<div style="float:left"><span class="optionValue"></span></div>
<div style="float:right; margin-top:11px;"><img src="../images/arrow_down.png" /></div>
<div class="clear"></div>
</div>
<div id="yearSelectorOptions" class="option_list" style="width:160px; display:none; margin-top:0px;">
<ul>
<li><input type="radio" name="year" value="2" style="display:none;" alt="Winst en verlies" checked="checked" />Winst en verlies</li>
<li><input type="radio" name="year" value="1" style="display:none;" alt="Eindbalans" />Eindbalans</li>
</ul>
</div>
</div></div>
jquery看起来像这样:
$(".option_list ul li").click(function()
{
$(this).children('form input[type="radio"]').prop('checked', true);
value = $(this).children('input[type="radio"]:checked').attr('alt');
$(this).parents('.table_settings').children('.optionValue').text(value); }
我想要的是当我点击[li]时,点击的[li]的值进入类optionValue。
我想复制这个列表,这样它就可以在一页上处理更多的列表。
我希望有人能帮助我。
提前感谢!
//select all the `<li>` elements that are the children of the `<ul>` element that is the child of the `.options_list` element
$(".option_list").children('ul').children().click(function() {
//select the first ancestor element with the `.table_settings` class,
//then select the `.optionValue` element that is a descendant of the `.table_settings` element,
//then set it's text to that of the input element inside the `<li>` element clicked
$(this).closest('.table_settings').find('.optionValue').text($(this).children().val());
});
下面是一个演示:http://jsfiddle.net/rpVxV/
试试这个:
$('.option_list')
.find('li')
.click(function () {
var $radio = $(this).children(':radio'),
value = $radio.attr('alt'), // Or `$(this).text()` ...
$(this)
.parents('.table_settings')
.find('.optionValue')
.text(value);
});
你的代码基本上不起作用,因为这一行:
//`.optionValue` is not IMMEDIATE
// children of `.table_settings`
// so it won't work
$(this).parents('.table_settings').children('.optionValue').text(value); }
相关文章:
- JavaScript:如何在最接近值的关联数组中查找键
- javascript代码查找最接近零的tescase
- 如何查找最接近内部文本的标记
- 使用最接近查找文本输入的JQuery问题
- Jquery - 按类查找最接近的文本
- 在 AngularJS 中查找最接近的元素
- 通过其类查找最接近的跨度到 TR 中
- 查找具有特定数据属性 jquery 的最接近的上一个元素
- 按键查找数组中最接近的 2 个元素
- 使用jQuery查找最接近后代的方法更有效
- 查找最接近的单选按钮集的值
- 查找包含 .myClass 的最接近的前一个同级
- jQuery查找最接近的
- javascript/查找数组中最接近的值
- 如何使用jquery查找最接近的元素
- Jquery-查找起始元素与该选择器最接近的父元素匹配的所有元素
- 在Javascript中查找最接近所需值的数组条目
- 查找最接近的类
- 使用5个日期对象javascript查找最接近当前时间的时间
- HTML/jQuery查找最接近<p>标签