Jquery checkbox help get name and value of checkbox

Problem
I'm trying to run some ajax on one of my pages for my site, basically I have three checkboxes, all of which are not selected on pageload when the checkbox is checked. I need to be able to load the appropriate HTML through ajax. This system is currently a PHP script that, depending on what POST is installed, returns a different view, so I think that all I need is to send POST via AJAX, but I need to do it every time a new checkbox is selected .

My HTML looks like this:

    div class="segment">
               <div class="label">
                <label>Choose region: </label>
               </div>


<div class="column w190">
                    <div class="segment">
                        <div class="input">
                            <input type="checkbox" checked="checked" class="radio checked" value="Y" name="area[Nationwide]" id="inp_Nationwide">
                        </div>
                        <div class="label ">
                            <label for="inp_Nationwide">Nationwide</label>
                         </div>
                        <div class="s">&nbsp;</div>
                    </div>

</div>

<div class="column w190">
                    <div class="segment">
                        <div class="input">
                            <input type="checkbox" checked="checked" class="radio checked" value="Y" name="area[Lancashire]" id="inp_Lancashire">
                        </div>
                        <div class="label ">
                            <label for="inp_Lancashire">Lancashire</label>
                         </div>
                        <div class="s">&nbsp;</div>
                    </div>

</div>

<div class="column w190">
                    <div class="segment">
                        <div class="input">
                            <input type="checkbox" checked="checked" class="radio" value="Y" name="area[West_Yorkshire]" id="inp_West_Yorkshire">
                        </div>
                        <div class="label ">
                            <label for="inp_West_Yorkshire">West Yorkshire</label>
                         </div>
                        <div class="s">&nbsp;</div>
                    </div>
               <div class="s">&nbsp;</div>
       </div>
</div>

My current attempt was to find out if the input was pressed, so I did it with my javascript, although this is probably wrong,

$('input.radio').click(function(){
        if($(this).hasClass('clicked')) {
            $(this).removeClass('clicked');
        } else {
            $(this).addClass('clicked');
        }
    });
+3
3

    $('input.radio').change(function(){ // will trigger when the checked status changes
            var checked = $(this).attr("checked"); // will return "checked" or false I think.
            // Do whatever request you like with the checked status
    });

,

, pageload

checked=""

+1
$("input:checkbox:checked")

.

.change() , / , , click().

$(".radio:checkbox").change(function() {
     var boxChecked = $(this).is(":checked");
     if(boxChecked) {
         ...do ajax...
      }
 });

, , toggle(). , html, ? ?

+1
$('input:checkbox').bind('click', function(e){
       switch(e.target.id){
           case 'someid':{
                 if($(this).is(':checked')){
                    //ajax call here
                 }
                 break;
           }
           case 'anotherid':{
                 // something
                 break;
           }
       }
    });

, , , .. hmm

0

Source: https://habr.com/ru/post/1740745/


All Articles