jQuery .data 方法 - 根据存储数据动态添加或删除元素
我使用对复选框值的更改进行的参数过滤创建了一个搜索页面。
$('#parameters input[type="checkbox"]:enabled').bind('change', function(e) {
var $this = $(this),
$checkedBoxes = $('#parameters').find('input:checkbox').filter(':checked').length,
index = $('#parameters input:checkbox').index($this),
txt = jQuery.trim($this.parent().text());
if ($checkedBoxes === 0) {
//remove all filters
$('#filters > ul > li').remove();
} else {
if ($this.is(':checked')) {
// add filter text
var filterLink = $('<li>' + txt + '<span>Remove filter</span></li>');
$.data(filterLink, 'myIndex', index);
alert($.data(filterLink, 'myIndex'));
$('#filters > ul').append(filterLink);
} else {
// remove filter text
$('#filters > ul').find('li[class=' + index + ']').remove();
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<ul id="parameters">
<li>
<label for="premium">
<input type="checkbox" id="premium" name="filters" value="true" />Premium
</label>
</li>
<li>
<label for="articles">
<input type="checkbox" id="articles" name="articles" value="true" />Articles
</label>
</li>
<li>
<label for="news">
<input type="checkbox" id="news" name="news" value="true" />News
</label>
</li>
<li>
<label for="other">
<input type="checkbox" id="other" name="other" value="true" />Other
</label>
</li>
</ul>
<div id="filters">
<p>Filters applied:</p>
<ul>
<!-- Filters applied insert here -->
</ul>
</div>
我正在显示动态列表中应用了哪个过滤器,并使用 jQuery 的 .data() 方法将关联存储在列表过滤器元素上。
现在,当我取消选中相应的复选框时,我想根据存储在所述元素上的数据删除过滤器 li 元素。我之前在类属性上执行此操作,但认为使用 .data() 是一个更简洁的解决方案。
不太清楚如何删除适当的列表元素?
I have created a search page using parametric filtering conducted on change of checkbox values.
$('#parameters input[type="checkbox"]:enabled').bind('change', function(e) {
var $this = $(this),
$checkedBoxes = $('#parameters').find('input:checkbox').filter(':checked').length,
index = $('#parameters input:checkbox').index($this),
txt = jQuery.trim($this.parent().text());
if ($checkedBoxes === 0) {
//remove all filters
$('#filters > ul > li').remove();
} else {
if ($this.is(':checked')) {
// add filter text
var filterLink = $('<li>' + txt + '<span>Remove filter</span></li>');
$.data(filterLink, 'myIndex', index);
alert($.data(filterLink, 'myIndex'));
$('#filters > ul').append(filterLink);
} else {
// remove filter text
$('#filters > ul').find('li[class=' + index + ']').remove();
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<ul id="parameters">
<li>
<label for="premium">
<input type="checkbox" id="premium" name="filters" value="true" />Premium
</label>
</li>
<li>
<label for="articles">
<input type="checkbox" id="articles" name="articles" value="true" />Articles
</label>
</li>
<li>
<label for="news">
<input type="checkbox" id="news" name="news" value="true" />News
</label>
</li>
<li>
<label for="other">
<input type="checkbox" id="other" name="other" value="true" />Other
</label>
</li>
</ul>
<div id="filters">
<p>Filters applied:</p>
<ul>
<!-- Filters applied insert here -->
</ul>
</div>
I am displaying which filter was applied in a dynamic list and storing the association on the list filter element with jQuery's .data() method.
Now, when I un-check the appropriate checkbox I want to remove the filter li element based on the data that is stored on the said element. I was previously doing this on the class attribute but thought using .data() was a neater solution.
Can't quite figure out how to remove the appropriate list element?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在最后一个选择器中,使用
而不是
查看我的更新小提琴: http://jsfiddle.net/sgGY3/
In your last selector, use
instead of
See my update fiddle: http://jsfiddle.net/sgGY3/
为什么使用
class
?您从未定义过类名。有
$(this).data
可以无缝工作:http:// jsfiddle.net/s9FjY/7/。您不需要为 0 个选中的复选框提供单独的代码。
代码:
Why are you using
class
? You never defined class names.There is
$(this).data
which works seamlessly: http://jsfiddle.net/s9FjY/7/.You do not need separate code for 0 checked checkboxes.
The code: