[英]Jquery getting the correct value based on checkbox checked
I cannot get this simple code to work. 我无法让这个简单的代码工作。 I want to get the value from the data attribute and based on that pass it to the alert 我想从data属性中获取值,并根据它将其传递给警报
http://jsfiddle.net/btL9C/ http://jsfiddle.net/btL9C/
$("input[type=checkbox]").change(function() {
var section_list = $('.section').data('section_list');
if ($(this).hasClass(section_list+"_list")) {
alert(section_list);
}
});
html: HTML:
<input type="checkbox" data-section_list = "1" class="section 1_list">
<input type="checkbox" data-section_list = "2" class="section 2_list">
<input type="checkbox" data-section_list = "3" class="section 3_list">
How can I get the alert to display the corresponding value of data-section_list? 如何获取警报以显示data-section_list的相应值?
Try this 尝试这个
$("input[type=checkbox]").change(function() {
var section_list = $(this).data().section_list;
if ($(this).hasClass(section_list+"_list")) {
alert(section_list);
}
});
Try to use $(this)
reference while fetching the data
from the current element, 尝试从当前元素获取data
时使用$(this)
引用,
$("input[type=checkbox]").change(function () {
var section_list = $(this).data('section_list');
if ($(this).hasClass(section_list + "_list")) {
alert(section_list);
}
}); //-- You have missed to mention the close parenthesis here.
change
function. 缺少paranthesis最后一行 - change
功能。 $(this)
to get data. 还可以使用$(this)
来获取数据。 $(document).ready(function(){
$("input[type=checkbox]").change(function() {
var section_list = $(this).data('section_list');
if ($(this).hasClass(section_list+"_list")) {
alert(section_list);
}
}); // <-- Missing
});
you need to get current clicked element data attribute by using this
currently you are getting using $(".section")
which will not give desired output as there are multiple elements on page with this class: 您需要通过使用来获取当前点击的元素数据属性, this
你目前正在使用越来越$(".section")
这将不会给所需的输出,因为有页面上的多个元素使用这个类:
change: 更改:
var section_list = $(".section").data('section_list');
to: 至:
var section_list = $(this).data('section_list'); //<--- gets current cliked element data attribute
Your code will look like: 您的代码将如下所示:
$("input:checkbox").change(function() {
var section_list = $(this).data('section_list');
if ($(this).hasClass(section_list+"_list")) {
alert(section_list);
}
});
You need to get the attribute of the element you are clicking on 您需要获取要单击的元素的属性
$("input[type=checkbox]").change(function() {
var section_list = $(this).attr('data-section_list');
if ($(this).hasClass(section_list+"_list")) {
alert(section_list);
}
}
Try this: use this instead .section after change function 试试这个:在改变功能之后使用它代替.section
<script type="text/javascript">
$(document).ready(function() {
$("input[type=checkbox]").change(function() {
var section_list = $(this).data('section_list');
if ($(this).hasClass(section_list+"_list")) {
alert(section_list);
}
});
});
</script>
Try this:- 尝试这个:-
$("input[type=checkbox]").click( function(){
if( $(this).is(':checked') ) alert($(this).attr("data-section_list"));
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.