locked
Save button state in the database. RRS feed

  • Question

  • User-34860367 posted

    Hello all,

    I used three buttons to perform the RadioButtonList control as following:

    <div id="TrafficLight">
    <label class="OFF"><input type="radio" name="toggle" checked="checked"  value="0"><span>OFF</span></label>
    <label class="ON"><input type="radio" name="toggle" value="1"><span>ON</span></label>
    <label class="FLASHING"><input type="radio" name="toggle" value="2"><span>FLA</span></label>
    </div>

    How do I know which button that I select when I click it, then save it to the database using value 0,1,2. Thanks.

    Thursday, December 15, 2016 1:38 AM

Answers

  • User1724605321 posted

    Hi avt2k7,

    Using Jquery  , you could try :

     <script type="text/javascript">
            $(function () {
                $('input[name=toggle]').on('change', function () {
                    alert($('input[name=toggle]:checked').val());
                });
            });
        </script>

    Html markup:

    <div id="TrafficLight">
                <label class="OFF">
                    <input type="radio" name="toggle" checked="checked" value="0" /><span>OFF</span></label>
                <label class="ON">
                    <input type="radio" name="toggle" value="1" /><span>ON</span></label>
                <label class="FLASHING">
                    <input type="radio" name="toggle" value="2" /><span>FLA</span></label>
            </div>

    Best Regards

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 15, 2016 3:30 AM

All replies

  • User1724605321 posted

    Hi avt2k7,

    Using Jquery  , you could try :

     <script type="text/javascript">
            $(function () {
                $('input[name=toggle]').on('change', function () {
                    alert($('input[name=toggle]:checked').val());
                });
            });
        </script>

    Html markup:

    <div id="TrafficLight">
                <label class="OFF">
                    <input type="radio" name="toggle" checked="checked" value="0" /><span>OFF</span></label>
                <label class="ON">
                    <input type="radio" name="toggle" value="1" /><span>ON</span></label>
                <label class="FLASHING">
                    <input type="radio" name="toggle" value="2" /><span>FLA</span></label>
            </div>

    Best Regards

    Nan Yu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 15, 2016 3:30 AM
  • User527778624 posted

    Hi,

    Try this JS code:

    window.onload=function(){
      document.querySelectorAll("#TrafficLight input[type='radio']")
       .forEach(function(x){ x.addEventListener('change',func,false); });  
    };
    
    function func(){
      if(this.checked){
        alert(this.value);
        //call ajax here to send value
      }
    }

    Thursday, December 15, 2016 12:45 PM