Javascript 如何查找单击的项目是否具有特定的数据属性值
我有这两个单选按钮Javascript 如何查找单击的项目是否具有特定的数据属性值,javascript,jquery,html,Javascript,Jquery,Html,我有这两个单选按钮 <input id="119715_0" type="radio" name="119715" value="76952" data-logic="yes" required /><label for="119715_0">Yes</label> <input id="119715_1" type="radio" name="119715" value="76953" data-logic="no" required />&l
<input id="119715_0" type="radio" name="119715" value="76952" data-logic="yes" required /><label for="119715_0">Yes</label>
<input id="119715_1" type="radio" name="119715" value="76953" data-logic="no" required /><label for="119715_1">No</label>
但是失败了您可以使用纯JavaScript:
if(element.getAttribute("data-logic") === null) {
// Doesn't have attribute
}
使用以下方法:
从jQuery1.4.3HTML5数据开始,属性将自动
拉入jQuery的数据对象。属性的处理
jQuery1.6中修改了嵌入式破折号,以符合W3CHTML5
规格
使用以下功能进行检查:
在我的原始代码中,
if($(this).data('logic'))
未按您的要求工作的用例是if值为0。我确信值false也会破坏它。您应该使用类型检查这些值。Javascript在类型上确实是错误的,因此您需要格外小心:
if (typeof($(this).attr('data-type')) === "undefined")
当然,这取决于包含真实值的
数据逻辑?
$("input").click(function() {
var dataAttr = $(this).attr("data-logic");
if (dataAttr == "yes") {
var value = $(this).attr("value");
alert('Has logic ' + value);
}
});
$("input").on("click", function(){
if($(this).data("logic"))
alert("haslogic");
});
$('input[name="119715"]').click(function() {
if ($(this).data('logic') !== undefined) {
alert('Has data-logic');
} else {
alert('Does not have data-logic');
}
});
if (typeof($(this).attr('data-type')) === "undefined")