Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jquery - saving values of toggle(yes or no) in database php

i am trying to use toggle buttons to save response in db as yes or no. for some reason the only response i am getting is 'on'. even when i switch off the button. i tried searching for problem and got a match but the problem was asked for android platform.now i am stuck with no answer there where similar questions but none of them is useful for me at this moment. sharing the code down below.Thanks in advance for those who are going to suggest or provide a solution.i am using class handicap to save data into variable inside JQUERY and then send that variable to AJAX page to perform db operation.i am not sharing CSS for toggle as i don't think that is required right now. if u need any additional info, do inform me.this input is inside a form with method POST. i am using a submit button with id that is calling this JQUERY. html part

<div class="switch">
    <input id="cmn-toggle-4" class="cmn-toggle cmn-toggle-round-flat handicap" type="checkbox" name="handicap">
    <label for="cmn-toggle-4"></label>
    </div>

jquery

     $("#save-medical-1").click(function () {
        var m11 = $(".handicap").val();   
    alert(m11);
        $.ajax({
          url: "ajexupdate.php",
          type: "POST",
          data: {smsgs11: m11},
          dataType: 'text',
          cache: false,
          success: function (e) {
            //           alert(e);  
            $("#user_medical_form").html(e);
            $("#medidetail").modal('hide');
            $('body').removeClass('modal-open');
            $('.modal-backdrop').remove();

          }
        });
        return false;
      });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can get value using ":checked" using jquery. eg.

if($("#cmn-toggle-4").is(":checked")){
m11="yes";
}
else{
m11="no";
}

and send it through ajax.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...