はい/いいえの2つのオプションを持つ選択ドロップダウンがあります(実際には3つ:値のない「オプションを選択」もあります)。次に、いくつかの変数を処理するいくつかのチェックボックスがあります (チェックされている場合、変数はグローバル変数 (tot と呼ばれる) に追加され、チェックされていない場合は減算されます)。ドロップダウン オプションが yes か no かに応じて、チェックボックスの 2 つの異なる動作が必要です。だから私はこれを書いたが、私は何かを誤解/誤解していると思う.
<select name="container" id="container">
<option id="none" value="" >Select an option</option>
<option id="no" value="no" >no</option>
<option id="yes" value="yes" >yes</option>
</select>
そして、これがjavascriptです(jqueryライブラリを使用)
var tot = 0;
$(document).ready(function () {
$("#container").change(function () {
var sel = $(this).find(":selected").attr("id")
if (sel == "no" || sel == "none") {
$("#in-category-1").click(function () {
if ($('#in-category-1').is(':checked')) {
if (tot < 60) {
tot = tot + 2.5;
} else if (tot >= 60) {
alert('Stop!');
$('#in-category-1').attr('checked', false);
}
} else if (tot > 0) {
tot = tot - 2.5;
}
functionblablabla();
});
if (sel == "yes") {
$("#in-category-1").click(function () {
if ($('#in-category-1').is(':checked')) {
if (tot < 50) {
tot = tot + 2.5;
} else if (tot >= 50) {
alert('Stop!');
$('#in-category-1').attr('checked', false);
}
} else if (tot > 0) {
tot = tot - 2.5;
}
functionblablabla2();
});
});
});
ページの読み込み時にデフォルトでオプションが選択されている場合でも何かが起こるはずです (オプションはデータベースに保存されます)。
if (($('#no').prop('defaultSelected')) || ($('#none').prop('defaultSelected')) ) {
$("#in-category-1").click(function () {
//same stuff than change select no or select none
});
}
if ($('#yes').prop('defaultSelected')) {
$("#in-category-1").click(function () {
//same stuff than change select yes
});
}
私は何を間違っていますか?