2015-11-07 32 views
0

我隱藏默認複選框,並使用一個div定製複選框圖片來代替:變化的背景圖像時復​​選框被選中

<aui:form> 
<c:if 
    test="<%=company.isAutoLogin() && !PropsValues.SESSION_DISABLED%>"> 
    <div class="rememberImage ftr" id="rememberImg"> 
     <aui:input checked="<%=rememberMe%>" name="rememberMe" id="rememberMe" 
          type="checkbox" cssClass="remember"/> 
    </div> 
</c:if> 
</form> 

//omiited 

<aui:script use="aui-base">  
     function changeBG() { 
      if (this.checked) { 
       document.getElementById('rememberImg').style.backgroundImage = 'url(../img/chk_none.png)'; 
      } else { 
       document.getElementById('rememberImg').style.backgroundImage = 'url(../img/chk_check.png)'; 
      }  
     } 

     document.getElementById('_58_rememberMe').addEventListener('change', changeBG); 


     var password = A.one('#<portlet:namespace />password'); 

     if (password) { 
      password.on(
       'keypress', 
       function(event) { 
        Liferay.Util.showCapsLock(event, '<portlet:namespace />passwordCapsLockSpan'); 
       } 
      ); 
     } 
    </aui:script> 

這並不在所有的工作。有什麼建議麼??非常感激!

更新:增加更多的代碼,我想也許有問題

+0

請提供jsfiddle – Alex

+0

它做什麼或不做什麼?有沒有任何控制檯輸出?從哪裏調用JavaScript,在您提供的HTML中似乎沒有任何參考。 – blm

+0

你應該添加onchange事件監聽器,所以你的條件語句不會是靜態的並且與用戶活動相關 – markoffden

回答

3

我看到了一個正確的答案已經超過,但要避免入侵的HTML標籤與JS的聽衆,有什麼被視爲不是最好的做法,我將爲你提供這種解決方案

function changeBG() { 
    if (this.checked) { 
     document.getElementById('myElement').style.backgroundImage = 'url(.....)'; 
    } else { 
     document.getElementById('myElement').style.backgroundImage = 'url(.....)'; 
    }  
} 

document.getElementById('myInput').addEventListener('change', changeBG); 

希望它會幫助你:)

+0

://的jsfiddle。淨/ o03q95g0 /)的jsfiddle和它,除非已經在使用它,使用jQuery對於這個簡單的任務工作正常 – markoffden

0

你可以試試這個:

document.getElementById('rememberImage').style.background = 'url(../img/chk_check.png)'; 
+0

雖然我不認爲問題出現在這一行,但我曾嘗試過您的建議,但沒有任何發生。 –

1

你可以這樣說:一個onclick屬性添加到複選框觸發一個切換功能。正如我不能與你的代碼測試(缺少休息)我只能爲你提供在身體背景得到改變

<input id="check" type="checkbox" onclick="toggle();"> Click me 

<script> 

    function toggle() { 

     if(document.getElementById("check").checked) { 
       document.getElementsByTagName("body")[0].style.backgroundColor="red"; 
     } 
    } 

</script> 
+0

是有道理的,但它不起作用 –

+0

它適用於我,所以你可能在你的代碼中有另一個錯誤。你能提供整個代碼還是在控制檯中尋找可能的js錯誤? – weidler

+0

整個JSP文件是很長,困惑,我想補充一些,你可以看看 –

1

div{ 
 
    margin: 20px 0; 
 
} 
 
input[type=checkbox] { 
 
    display: none 
 
} 
 
input[type=checkbox]+label { 
 
    background: url(http://s17.postimg.org/phsoii5vf/check.png) no-repeat; 
 
    padding-left: 30px; 
 
    padding-bottom: 5px; 
 
    padding-top: 2.5px; 
 
    height: 18px; 
 
} 
 

 
input[type=checkbox]:checked+label { 
 
    background: url(http://s2.postimg.org/zbjg138np/check_tick.jpg) no-repeat;  
 
}
<div> 
 
    <input type="checkbox" id="chk1"> 
 
    <label for="chk1">Custom Checkbox1</label> 
 
</div> 
 
<div> 
 
    <input type="checkbox" id="chk2"> 
 
    <label for="chk2">Custom Checkbox2</label> 
 
</div> 
 
<div> 
 
    <input type="checkbox" id="chk3"> 
 
    <label for="chk3">Custom Checkbox3</label> 
 
</div> 
 
<div> 
 
    <input type="checkbox" id="chk4"> 
 
    <label for="chk4">Custom Checkbox4</label> 
 
</div>

不需要javascript.you能做到這一點的enxample來自css。

<div> 
<input type="checkbox" id="chk"> 
<label for="chk">Custom Checkbox1</label> 
</div> 

input[type=checkbox] { 
    display: none 
} 
input[type=checkbox]+label { 
    background: url(../images/check.png) no-repeat; 
    padding-left: 30px; 
    padding-bottom: 5px; 
    padding-top: 2.5px; 
    height: 18px; 
} 

input[type=checkbox]:checked+label { 
    background: url(../images/check_tick.png) no-repeat;  
} 
+0

的beckground是從另一個div輸入的不是背景,並經過與[這](HTTPS標籤 –

0

function SetBackground(elm){ 
 
    if($(elm).is(":checked")) 
 
    { 
 
    $("#rememberImage").css({"background-color":"gray"}); 
 
     } 
 
    else 
 
    { 
 
    $("#rememberImage").css({"background-color":"white"}); 
 
    } 
 
    
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div class="rememberImage ftr" id="rememberImage">    
 
    <input checked="<%=rememberMe%>" name="rememberMe" id="rememberMe" type="checkbox" onchange="SetBackground(this)"/>       
 
</div>

我不知道你的自定義複選框是如何工作的,但是,你可以添加onchange事件並檢查複選框是否被選中,然後更改div的背景。

祝你好運

+0

是矯枉過正,而且比硬編碼樣式信息將其移動到頁面的CSS和使用類更好。 – blm