2017-05-10 1 views
2

Ich ersetzte die Schaltfläche auf dem jQuery Toggle-Code mit einer Checkbox, aber es kann nicht funktionieren. HierErsetzt Toggle-Taste mit einem Kontrollkästchen, aber nicht funktioniert

ist die HTML-Datei, in der ich versuchte, den Knopf mit einem Kontrollkästchen zu ersetzen

index.html

<p> hi </p> 
<!--<button>Press me</button>--> 
<div class="onoffswitch"> 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
    <label class="onoffswitch-label" for="myonoffswitch"></label> 
</div> 

javascript

$(function() { 

    if (Cookies) { 
     $("p").toggle(!(!! Cookies("toggle-state")) || Cookies("toggle-state") === 'true'); 
    } 

    // $('button').on('click', function() 
    $('#myonoffswitch.onoffswitch').is(":checked"), function() 
    { 
     $("p").toggle(); 
     Cookies.set("toggle-state", $("p").is(':visible'), { 
      expires: 1, 
      path: '/' 
     }); 
    }); 

}); 

Fiddle

Was ist hier falsch?

+0

Aber was wollen Sie das Ouput sein? Sie haben eine Frage gestellt, aber was erwarten Sie als Antwort? –

+0

@AlivetoDie, die

+0

@DeepakYadav, verstecken und zeigen auf Toggle – Mecom

Antwort

2

Eigentlich arbeiten Sie nicht mit Check-Box-Klasse überhaupt. deshalb funktioniert es nicht.

Do it wie folgt: -

Change-

$('#myonoffswitch.onoffswitch').is(":checked"), function() { 

Um

$('.onoffswitch-checkbox').click(function() { 

Und alles wird gut funktionieren.

Arbeitsbeispiel: - https://jsfiddle.net/2uz0rsq8/

Hinweis: - Sie verwenden können: - $('.onoffswitch-checkbox').change(function(){ auch (durch @NikhilNanjappa)

+0

Vielen Dank Sir, es funktioniert – Mecom

+0

@Mecom froh, Ihnen zu helfen :) :) –

+0

Nur um hinzuzufügen, können Sie auch '$ ('. Onoffswitch-checkbox') verwenden. Change (function() {' –

3

$(function() { 
 
    
 

 
     $('#myonoffswitch').click(function(){ 
 
       /*Use toggle if you need only hide and show */ 
 
       //$("p").toggle(); 
 
       
 
       /*If you want to know the current status of checkbox  go with below code*/ 
 
      if($(this).is(':checked')){ 
 
        $("p").show(); 
 
      }else{ 
 
        $("p").hide(); 
 
      } 
 
     }) 
 
    
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<p> hi </p> 
 
<!--<button>Press me</button>--> 
 
<div class="onoffswitch"> 
 
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked> 
 
    <label class="onoffswitch-label" for="myonoffswitch"></label> 
 
</div>

$('#myonoffswitch.onoffswitch').is(":checked"), function() 
    { 
     $("p").toggle(); 
     Cookies.set("toggle-state", $("p").is(':visible'), { 
      expires: 1, 
      path: '/' 
     }); 
    }); 

Änderung dieser Code

 $('#myonoffswitch').click(function(){ 
      /*Use toggle if you need only hide and show */ 
      //$("p").toggle(); 

      /*If you want to know the current status of checkbox go with below code*/ 
     if($(this).is(':checked')){ 
       $("p").show(); 
     }else{ 
       $("p").hide(); 
     } 
    }) 
+0

Ich ersetzte das gesamte Skript, aber es schaltet nicht um – Mecom

+0

@Mecom überprüfen Sie das Snippet, wenn Sie nach diesem suchen – XYZ

Verwandte Themen