简体   繁体   中英

Flipswitch updating mysql using jquery-mobile

I'm trying to utilize the JQM Flipswitch but I'm encountering some problems. My goal: when the flipswitch is turned on or off → java notices → php → mysql

This code sort-of works but there are two issues: 1) Always returns OFF 2) Doesn't update

Any help would be appreciated

HTML

<fieldset>
   <div data-role="fieldcontain">
      <span id="alarm_box">
         <label for="alarm">Alarm:</label>
         <input type="checkbox" id="alarm" data-role="flipswitch">
         <div id="alarm_n"></div>
      </span>
   </div>
</fieldset>

JAVA/AJAX

$('#alarm_box').on('click', function () {
    var checkStatus = this.checked ? 'ON' : 'OFF';

    $.post("main_send.php", {
        "alarm": checkStatus
    },

    function (data) {
        $('#alarm_n').html(data);
    });
});

Use change event bound to Flipswitch ( alarm ) not click .

$('#alarm').on('change', function () {
    var checkStatus = this.checked ? 'ON' : 'OFF';

    $.post("main_send.php", {
        "alarm": checkStatus
    },

    function (data) {
        $('#alarm_n').html(data);
    });
});

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM