0

基本的な検証を行うために、チェックボックスがオンになっているかどうかを確認できる必要があります。問題: これを生成する PHP にアクセスできません。クラスが追加されていないか、ほとんどのフォームにある基本的な checked=checked はありません。チェックボックスをターゲットにする最も簡単な方法は何ですか?

http://www.inpresence.in/event-registration?ee=4

編集:フリークアウト!! これがコードです。チェックボックスをターゲットにするだけで、他のすべてが機能しています。jquery の :checked メソッドは、そこにないチェックボックス内で checked=checked を使用します。

   $(document).ready(function(){
    //when the submit button is clicked...
    $("input.btn_event_form_submit").click(function(){

    //find the value of the drop down with one evening or four evenings
    var priceOption = $("#price_option-4").val();

    //match a string ending with "one evening" as the first numbers will be randomly generated by php
    var oneEvening = /^\d{2}\|One Evening$/.test(priceOption);

    //match a string ending with "four evenings" as the first numbers will be randomly generated by php
    var fourEvenings = /^\d{2}\|Four Evenings$/.test(priceOption);

    //HOW DO I GET THE CHECKED BOXES?!
    var checkedBoxCount = $('#dates-1351733097 .valid').is(':checked').length;

    //if one evening is selected make sure the checked boxes count does in fact equal one
    if(oneEvening && checkedBoxCount != 1){
        //if it doesn't alert the user and return false
        alert('You must select one date');
        return false;
    }

    //if one evening isn't selected, four is. make sure the count does indeed in 4
    else if (fourEvenings && checkedBoxCount != 4){
        //if it doesnt alert the user and return to the form
        alert('You must select four dates');
        return false;
    }

    //else, everything checks out!
    else {
        return;
    }

});
});
4

4 に答える 4

2

これを解決するためにjqueryを使用してみましたか? http://jquery-howto.blogspot.co.uk/2008/12/how-to-check-if-checkbox-is-checked.html

$('#edit-checkbox-id').is(':checked'); 
于 2012-11-05T17:59:45.427 に答える
2

この JavaScript コードを使用すると、チェックボックスがオンになっているかどうかを確認できます。

var isChecked = document.getElementById("my-checkbox").checked;

またはjQueryを使用して:

var isChecked = $('#my-checkbox').is(':checked');

編集:これを試して、うまくいくかどうか教えてください:

var checkedBoxCount = $('#dates-1351733097 .valid:checked').length;
于 2012-11-05T18:02:14.490 に答える
0

jquery:checkedセレクターを使用します。http://api.jquery.com/checked-selector/

于 2012-11-05T18:00:08.897 に答える
0

これにより、JavaScriptで必要なブール値が得られます。

document.getElementById("Nov.12-4_1").checked

ソースを表示し、要素を見つけて、それらが持つIDを表示できます。

その他の回答:OPは、jqueryの回答が必要であることを指定していませんでした。彼がこれまで何にもjqueryを使用していなかった場合。これだけのためにそれを追加するのは少しやり過ぎだと思います。

于 2012-11-05T18:00:41.533 に答える