[英]Check if element doesn't have attribute jQuery
onClick
of an item it adds an attribute. onClick
一项它添加一个属性。 Only three items can be selected so they'll only ever be three elements on the page with the attribute like : data-selected: 1
, data-selected: 2
or data-selected: 3
. 只能选择三个项目,因此页面上只有三个元素,其属性如下:
data-selected: 1
, data-selected: 2
或data-selected: 3
。
if ($('.item').attr('data-selected', '1') || $('.item').attr('data-selected', '1') ||
$('.item').attr('data-selected', '1')) {
}
I can do an if statement to check if an element has an attribute
, how do I check if all the elements with .item
class have an attribute and if not then do something. 我可以做一个if语句来检查一个元素是否有一个
attribute
,如何检查所有带有.item
类的元素.item
都有一个属性,如果没有,那么就做一些事情。
Use data() method instead of attr()
when you deal with data attributes, and you have to now the difference between data attribute setters and getters. 在处理数据属性时使用data()方法而不是
attr()
,现在你必须在数据属性设置器和getter之间有所区别。
Setter : 塞特:
$('.item').data('selected', '1'); // assign to the data attribute selected the value 1
Getter : 吸气:
$('.item').data('selected'); //get the value 1 from data attribute selected
Try to store the value of data attribute selected
in variable then check : 尝试存储在变量中
selected
的数据属性的值,然后检查:
var selected = $('.item').data('selected');
if (selected == 1 || selected ==2 || selected == 3) {
//Code here
}
how do I check if all the elements with .item class have an attribute and if not then do something.
如何检查.item类的所有元素是否具有属性,如果没有,则执行某些操作。
You could use Jquery not selector :not() that will selects all elements that do not match the given selector. 您可以使用Jquery而不是selector :not()将选择与给定选择器不匹配的所有元素。 :
:
if($('.item:not([data-selected])').length)
//Not all items has attribute data-selected
else
//All items has attribute data-selected
Hope this helps. 希望这可以帮助。
Just check that your selected elements have the required attribute (or not). 只需检查所选元素是否具有必需属性(或不具有)。 You can
.filter
or use the .not
traversal function. 您可以
.filter
或使用.not
穿越功能。
Based on what I understand of this question, you want to restrict selection to only three items. 根据我对此问题的理解,您希望将选择限制为仅三个项目。 Here's a proposal :
这是一个提案:
$(function () { $('.item').on('click', function () { var checked = $(this).prop('checked'); // currently checked? var selected = $('.item').filter('[data-selected]'); if (checked) { $(this).attr('data-selected', -1).parent().addClass('selected'); selected = selected.add(this); } else { $(this).removeAttr('data-selected').parent().removeClass('selected'); selected = selected.not(this); } selected.sort(function (a, b) { return $(a).attr('data-selected') - $(b).attr('data-selected'); }).each(function (index) { if (index >= 3) { $(this).prop('checked', false).removeAttr('data-selected').parent().removeClass('selected'); } else { $(this).attr('data-selected', index + 1); } }); }); });
.selected { color: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label><input type="checkbox" class="item">Item 1</label> <label><input type="checkbox" class="item">Item 2</label> <label><input type="checkbox" class="item">Item 3</label> <label><input type="checkbox" class="item">Item 4</label> <label><input type="checkbox" class="item">Item 5</label> <label><input type="checkbox" class="item">Item 6</label>
Note : you may replace .attr
and .removeAttr
with .data
and .removeData
, however only with the former that you may see changes on the DOM. 注意 :您可以使用
.data
和.removeData
替换.attr
和.removeAttr
,但只有前者才能看到DOM上的更改。 jQuery data (ie .data
) only keep internal cache of the data and does not affect the DOM tree. jQuery数据(即
.data
)仅保留数据的内部缓存,不会影响DOM树。 Either is fine in your case and this is a matter of choice. 在你的情况下,这两个都很好,这是一个选择的问题。
how do I check if all the elements with .item class have an attribute and if not then do something.
如何检查.item类的所有元素是否具有属性,如果没有,则执行某些操作。
var items = $('.item');
var itemsWithoutAttribute = items.filter(':not([data-selected]');
if(itemsWithoutAttribute.length > 0) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.