2016-04-13 6 views
2

まず、あいまいな質問をおかけして申し訳ありません。私の問題は、例で最もよく説明されています。カスタムCSSチェックボックスjQueryによって誘発された「チェック済み」プロパティがチェックボックスを視覚的に「チェック」していません

私はいくつかのチェックボックスがあるブートストラップドロップダウンメニューを使用しています。私はいくつかの素晴らしいFontAwesomeのアイコンを使用して、CSSのトリックを使用して外観を変更しています。しかし、私はjQueryのを使用して、これらのチェックボックスの状態で何かをやろうとしているとき、最初のチェック/アンチェックが動作しているようですが、それ以降のトグルは(チェックを外します/チェック)...動作しません

$('.js-inputhit').click(function() { 
 
    if (this === event.target) { 
 
    $(this).find(':input').click(); 
 
    } 
 
}); 
 
$('.js-inputhit :input').click(function(e) { 
 
    e.stopPropagation(); 
 
}); 
 

 
$('.selectchat_all').click(function() { 
 
    $('.selectchat_active').attr('checked', false); 
 
    $('#chat1, #chat2, #chat3, #chat4').attr('checked', true); 
 
}); 
 
$('.selectchat_active').click(function() { 
 
    $('.selectchat_all').attr('checked', false); 
 
    $('#chat1, #chat2, #chat3, #chat4').attr('checked', false); 
 
});
/*Adding custom checkbox icons*/ 
 

 
label { 
 
    position: relative; 
 
    padding-left: 20px; 
 
    cursor: pointer; 
 
} 
 
label:before, 
 
label:after { 
 
    font-family: FontAwesome; 
 
    font-size: 14px; 
 
    /*absolutely positioned*/ 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
} 
 
label:before { 
 
    content: '\f096'; 
 
    /*unchecked*/ 
 
} 
 
label:after { 
 
    content: '\f046'; 
 
    /*checked*/ 
 
    /*checked icon will be hidden by default by using 0 max-width and overflow hidden*/ 
 
    max-width: 0; 
 
    overflow: hidden; 
 
    opacity: 0.5; 
 
    /*CSS3 transitions for animated effect*/ 
 
    transition: all 0.35s; 
 
} 
 
/*hiding the original checkboxes*/ 
 

 
input[type="checkbox"] { 
 
    display: none; 
 
} 
 
/*when the user checks the checkbox the checked icon will animate in*/ 
 

 
input[type="checkbox"]:checked + label:after { 
 
    max-width: 25px; 
 
    /*an arbitratry number more than the icon's width*/ 
 
    opacity: 1; 
 
    /*for fade in effect*/ 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" /> 
 
<input type="checkbox" name="chat1" id="chat1" checked/> 
 
<label for="chat1" style="font-weight: normal; margin-bottom: 0px;">DreamhackCS</label> 
 
<br> 
 

 
<input type="checkbox" name="chat2" id="chat2" /> 
 
<label for="chat2" style="font-weight: normal; margin-bottom: 0px;">IzakOOO</label> 
 
<br> 
 

 
<input type="checkbox" name="chat3" id="chat3" /> 
 
<label for="chat3" style="font-weight: normal; margin-bottom: 0px;">Nightblue3</label> 
 
<br> 
 

 
<input type="checkbox" name="chat4" id="chat4" /> 
 
<label for="chat4" style="font-weight: normal; margin-bottom: 0px;">imaqtpie</label> 
 
<br><br> 
 

 
<input type="checkbox" name="active" class="selectchat_active" id="active" /> 
 
<label for="active" style="font-weight: normal; margin-bottom: 0px;">Check none</label> 
 
<br> 
 

 
<input type="checkbox" name="all" class="selectchat_all" id="all"/> 
 
<label for="all" style="font-weight: normal; margin-bottom: 0px;">Check all</label>
この例では

問題: をクリックすると、すべての4つのチャンネルをチェックし、 "すべてをチェック"。 "チェックなし"をクリックすると、すべてチェックが外されます。 (ここまでは順調ですね)。しかしその後、「すべてをチェック」は機能しなくなりました。それはもはや4チャンネルをチェックしません。 "チェックなし"でもすべてをチェックします(手動でチェックした場合)。 "すべてチェック"は機能しなくなりました...

誰かが私の問題を見つけることができますか?ご協力いただきありがとうございます。

答えて

7

使用.prop()の代わりに.ATTR()の

のattr()のみを参照、詳細は実際のDOMツリー

を更新http://api.jquery.com/prop/this answer

$('.js-inputhit').click(function() { 
 
    if (this === event.target) { 
 
    $(this).find(':input').click(); 
 
    } 
 
}); 
 
$('.js-inputhit :input').click(function(e) { 
 
    e.stopPropagation(); 
 
}); 
 

 
$('.selectchat_all').click(function() { 
 
    $('.selectchat_active').prop('checked', false); 
 
    $('#chat1, #chat2, #chat3, #chat4').prop('checked', true); 
 
}); 
 
$('.selectchat_active').click(function() { 
 
    $('.selectchat_all').prop('checked', false); 
 
    $('#chat1, #chat2, #chat3, #chat4').prop('checked', false); 
 
});
/*Adding custom checkbox icons*/ 
 

 
label { 
 
    position: relative; 
 
    padding-left: 20px; 
 
    cursor: pointer; 
 
} 
 
label:before, 
 
label:after { 
 
    font-family: FontAwesome; 
 
    font-size: 14px; 
 
    /*absolutely positioned*/ 
 
    position: absolute; 
 
    top: 0; 
 
    left: 0; 
 
} 
 
label:before { 
 
    content: '\f096'; 
 
    /*unchecked*/ 
 
} 
 
label:after { 
 
    content: '\f046'; 
 
    /*checked*/ 
 
    /*checked icon will be hidden by default by using 0 max-width and overflow hidden*/ 
 
    max-width: 0; 
 
    overflow: hidden; 
 
    opacity: 0.5; 
 
    /*CSS3 transitions for animated effect*/ 
 
    transition: all 0.35s; 
 
} 
 
/*hiding the original checkboxes*/ 
 

 
input[type="checkbox"] { 
 
    display: none; 
 
} 
 
/*when the user checks the checkbox the checked icon will animate in*/ 
 

 
input[type="checkbox"]:checked + label:after { 
 
    max-width: 25px; 
 
    /*an arbitratry number more than the icon's width*/ 
 
    opacity: 1; 
 
    /*for fade in effect*/ 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" /> 
 
<input type="checkbox" name="chat1" id="chat1" checked/> 
 
<label for="chat1" style="font-weight: normal; margin-bottom: 0px;">DreamhackCS</label> 
 
<br> 
 

 
<input type="checkbox" name="chat2" id="chat2" /> 
 
<label for="chat2" style="font-weight: normal; margin-bottom: 0px;">IzakOOO</label> 
 
<br> 
 

 
<input type="checkbox" name="chat3" id="chat3" /> 
 
<label for="chat3" style="font-weight: normal; margin-bottom: 0px;">Nightblue3</label> 
 
<br> 
 

 
<input type="checkbox" name="chat4" id="chat4" /> 
 
<label for="chat4" style="font-weight: normal; margin-bottom: 0px;">imaqtpie</label> 
 
<br><br> 
 

 
<input type="checkbox" name="active" class="selectchat_active" id="active" /> 
 
<label for="active" style="font-weight: normal; margin-bottom: 0px;">Check none</label> 
 
<br> 
 

 
<input type="checkbox" name="all" class="selectchat_all" id="all"/> 
 
<label for="all" style="font-weight: normal; margin-bottom: 0px;">Check all</label>

+0

ありがとうございます!私は考えていなかった:)それは今働く。私ができる時にあなたの答えを正しいものとして選ぶ(7分後)。 –

+1

問題が解決してうれしいです。それは何のためのstackoverflowです:) –

2

.attrの代わりに.propを使用してみてください:

詳細については、this answerを参照してください。

+0

素晴らしい、それは動作します。違いを明確にしていただきありがとうございます。 –

関連する問題