javascript - 如何将 html 开/关翻转开关添加到 jquery?

标签 javascript jquery html css

我正在尝试将 html/css 与我的 jquery 一起使用,但无法使其正常工作。 截至目前,当我单击与“vlabe”匹配的 div 时,它将根据状态打开或关闭。但是我正在尝试使用来自 proto.io 的 html/css 开/关开关,但我无法让它工作。

如果我现在有 <div id='light'></div>它会根据状态显示“开”或“关”。我需要让它在 <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="" checked> 中添加/删除“选中”

switchclick='';
    if (vdata == 'Off' ) {


    switchclick = 'onclick="SwitchToggle('+item.idx+', \'On\');"';
            vdata = 'Off';

    } else {



    switchclick = 'onclick="SwitchToggle('+item.idx+', \'Off\');"';
            vdata = 'On';
    }




 if( SwitchType == 'On/Off') {
// code to be executed if condition is true
$('#'+vlabel).html('<div '+switchclick+' >'+vdata+'</div>');

}

我希望我解释了我自己和我想做的事情,如果没有,请告诉我,不要给我投反对票。我在尝试。 :)

完整脚本供引用。

    function RefreshData()
{
    clearInterval($.refreshTimer);
    var jurl=$.domoticzurl+"/json.htm?type=devices&plan="+$.roomplan+"&jsoncallback=?";
    $.getJSON(jurl,
    {
        format: "json"
    },
    function(data) {
    if (typeof data.result != 'undefined') {

    $.each(data.result, function(i,item){
    for( var ii = 0, len = $.PageArray.length; ii < len; ii++ ) {
    if( $.PageArray[ii][0] === item.idx ) {     // Domoticz idx number
    var vtype=  $.PageArray[ii][1];     // Domotitcz type (like Temp, Humidity)
    var vlabel= $.PageArray[ii][2]; // cell number from HTML layout
    var vdesc=  $.PageArray[ii][3]; // description 
    var vattr=  $.PageArray[ii][4]; // extra css attributes
    var valarm= $.PageArray[ii][5];     // alarm value to turn text to red
    var SwitchType= item.SwitchType;        // Switch type "Dimmer""On/Off" "ECT"
    var vdata=  item[vtype];        // current value




//For Dimmers, if a Dimmer is "off it shows off, If on it shows the percent with a %"
    if (item.Status == 'Off'){
        DimmerLVL = 'Off';
    } else {

        DimmerLVL = (+item.Level+ '%');
        }




    // create switchable value when item is switch
            switchclick='';
        if (vdata == 'Off' ) {


        switchclick = 'onclick="SwitchToggle('+item.idx+', \'On\');"';
                vdata = 'Off';

        } else {



        switchclick = 'onclick="SwitchToggle('+item.idx+', \'Off\');"';
                vdata = 'On';
        }




if( SwitchType == 'On/Off') {
    // code to be executed if condition is true
    $('#'+vlabel).html('<div '+switchclick+' >'+vdata+'</div>');

    } else if (SwitchType == 'Dimmer') {
    // code to be executed if condition is false
    $('#'+vlabel).html('<div>'+DimmerLVL+'</div>');

    } else {
    // code to be executed if condition is false
    $('#'+vlabel).html('<div>123'+vdata+'</div>');
}




    // scene alle verlichting uit: http://192.168.0.100:8080/json.htm?type=command&param=switchscene&idx=2&switchcmd=ON

    // if extra css attributes. Make switch not switchable when it is protected.




    $('#desc_'+vlabel).html(vdesc);

    }
    }
    });
    }
    });
    $.refreshTimer = setInterval(RefreshData, 8000);
    }


    $(document).ready(function() {
    $.roomplan=2;       // define roomplan in Domoticz and create items below.
    $.domoticzurl="http://192.168.1.125:8080";
    //format: idx, value, label, description, [override css], [alarm value]
    $.PageArray = [


    ['4',   'Status',       'cell6',    'Lamp',],
    ['2',   'Status',       'cell7',    'TV',],


    ];


RefreshData();


});  


function SwitchToggle(idx, switchcmd)
    {
     console.log('function called');
     $.ajax({
        url: "http://192.168.1.125:8080/json.htm?type=command&param=switchlight" + "&idx=" + idx + "&switchcmd=" + switchcmd + "&level=Level",
        async: false, 
        dataType: 'json',
        success: function(){
        console.log('SUCCES');
        },
        error: function(){
        console.log('ERROR');
        }
    });
    RefreshData();
    }

最佳答案

这对你有用吗?

$('#light').on('click', function() {
  
  var state = $('#light').html();
  var $checkbox = $('.onoffswitch-checkbox');

  $('#light').html(state == 'Off' ? 'On' : 'Off');

  $checkbox.prop('checked', !$checkbox.prop('checked'));
})
#light {
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


The checkbox:
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="" checked>

<br>
<br>Click the red div, The switch is:
<div id="light">On</div>

关于javascript - 如何将 html 开/关翻转开关添加到 jquery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584150/

相关文章:

javascript - 检测浏览器在调整大小和更新元素位置时宽度变化了多少

html - 如何正确设计html表单

javascript - 如何更改html输入的边框颜色

javascript - 真正安全的 `eval` 使用 ES6 `Proxy` 和 `with` ?

java - 如何使用 AJAX、JQuery 和 Spring MVC 渲染 View

javascript - angularjs 获取 DOM ng-model

javascript - Angular 可以在两个输入字段之间进行双向数据绑定(bind)吗?

javascript - 脚本中相减后的结果不同

jquery - CSS 图像叠加未到达侧面

html - 窗口大小减小时框内的文本超出