[英]Jquery check if checkbox checked
Somthing is wrong with this code. 这段代码有什么问题。
I have div box and when click on Coutries title he shows the list of countries. 我有div框,当单击Coutries标题时,他显示了国家列表。 that list contain 5 check box
该列表包含5个复选框
Fiddle http://jsfiddle.net/ikac/bfaH5/ 小提琴http://jsfiddle.net/ikac/bfaH5/
I have problem when i try to check if some countury checked.. 当我尝试检查是否有countury时出现问题。
searchContainer: function() {
var title = $(".title-search");
// On click show countury list
title.click(function(){
$(".state").fadeToggle('fast',function(){
// check if sothing checked
if ($("#france").is(':checked')) {
alert("France Selected");
// Show all cities from this countury
}
});
});
}
But when i check a country nothing happens.... 但是当我检查一个国家时,什么也没有发生。
First time i try this: 我第一次尝试这个:
if($("#france").checked = true)) {
alert("FRANCE");
}
But when i click on title and when fadeToggle is done he show alert(FRANCE) whitout checking. 但是,当我单击标题时,并且当fadeToggle完成时,他会显示alert(FRANCE)停止检查。
HTML : HTML:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="javascript/gfactory.js"></script>
<link rel="stylesheet" type="text/css" href="css/gfactory.css">
<script>
$(document).ready(function() {
window.Balcanrent.init();
window.Balcanrent.searchContainer();
});
</script>
</head>
<body>
<form name="search">
<div class="search-box">
<div class="title-search"> Counturies </div>
<div class="state">
<ul class="s_list">
<li><input id="ger" type="checkbox" value="gr">Germania</li>
<li><input id="usa" type="checkbox" value="usa">USA</li>
<li><input id="sr" type="checkbox" value="sl"> Serbia </li>
<li><input id="france" type="checkbox" value="fr">France </li>
</ul>
</div>
</div>
</form>
</body>
</html>
And i try to use .length > 0 but nothing again. 我尝试使用.length> 0,但再也没有。 What i do wrong?
我做错了什么?
Thanks. 谢谢。
If you prefer to use the CSS selector approach, you can do this: 如果您更喜欢使用CSS选择器方法,则可以执行以下操作:
var c = $('#france').is(":checked");
Or: 要么:
var c = $('#france').prop("checked");
.prop() is probably the better choice. .prop()可能是更好的选择。
From here: http://api.jquery.com/prop/ - is() and prop(), while they retrieve different things, both work reliably. 从这里开始: http : //api.jquery.com/prop/-is ()和prop(),虽然它们检索不同的内容,但是两者都能可靠地工作。 While using attr() has a different meaning - it retrieves only the default value
虽然使用attr()具有不同的含义-它仅检索默认值
I have created a fiddle here for you. 我在这里为您制作了一个小提琴。
http://jsfiddle.net/kyawsithu/shC3c/1/ http://jsfiddle.net/kyawsithu/shC3c/1/
Below code you used for checking the Checkbox checked property is correct. 您用于检查Checkbox选中的属性的以下代码正确。
if ($("#france").is(':checked')) {
alert("France Selected");
}
EDIT: When you click again on Countries when States fade in, the checked is still true and it shows the alert. 编辑:当国家淡入时再次单击“ 国家”时,选中的复选框仍然为true,并显示警报。
Hope this can help you.. 希望这可以帮到你..
var title = $(".title-search");
title.click( function(){
var selected = '';
$('.s_list input:checked').each(function() {
selected += $(this).attr('id') + ',';
});
$(".state").fadeToggle('fast');
if(selected.length)
alert('selected: ' + selected);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.