2016-04-16 19 views
-2

我試圖實現一個簡單的開/關按鈕。 我只想在鏈接(如果可能的話在背景中),當按鈕打開時,以及不同的鏈接,當它關閉 請幫助我在JavaScript中,因爲我是一個初學者。如果按鈕爲ON,則可以進入網站的功能

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch2" onclick="javascript:location.href='www.google.com" > 
<label class="onoffswitch-label" for="myonoffswitch2"> 

回答

-1

您可以添加一個onchange監聽器,並直接根據該複選框的選中狀態的用戶。

$(document).ready(function(){ 
 
    $('#myonoffswitch2').change(function(){ 
 
    if(this.checked){ 
 
     window.location="url to site 1"; 
 
    } 
 
    else{ 
 
     window.location="url to site 2"; 
 
    } 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch2"> 
 
<label class="onoffswitch-label" for="myonoffswitch2">Check</label>

-2
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch2" onclick="javascript:location.href='https://www.google.com';" > 

//此代碼按鈕上,但你是什麼意思掉當你的按鈕上,然後你的頁面重定向

相關問題