2013-07-31 156 views
1

我有這個代碼,但它只是不會工作。問題是什麼?我的語法錯了還是整個方法錯了?javascript改變文本框的顏色

<input checked="checked" class="form-field" id="IsCurrentlySmokingrightNej" name="IsCurrentlySmokingright" type="radio" value="Nej"> 
<input class="form-field" id="IsCurrentlySmokingrightJa" name="IsCurrentlySmokingright" type="radio" value="Ja"> 
    <input id="IsCurrentlySmokingrighttextbox" name="IsCurrentlySmokingright" type="text" style="background-color: rgb(0, 255, 0); float: right; width: 50px;"> 

的javascript:

$(document).on("change", "IsCurrentlySmokingrightNej", function() { 
    var elem = document.getElementById("IsCurrentlySmokingrightNej"); 
    var ele = $("#IsCurrentlySmokingrighttextbox"); 
    if (elem.checked) { 
     ele.css("background-color", "rgba(0, 255, 0, 1)"); 
     ele.css("float", "right"); 
     ele.css("width", "50px"); 

    } 
}); 

    $(document).on("change", "IsCurrentlySmokingrightJa", function() { 
     var elem = document.getElementById("IsCurrentlySmokingrightJa"); 
     var ele = $("#IsCurrentlySmokingrighttextbox"); 
     if(elem.checked){ 
      ele.css("background-color", "rgba(255, 0, 0, 1");//röd 
      ele.css("float", "right"); 
      ele.css("width", "50px"); 
     } 

}); 
+0

之前'IsCurrentlySmokingrightJa'添加一個''#,所以'$(文件)。在( 「變」, 「#IsCurrentlySmokingrightJa」 ...' – putvande

回答

3

你在ID選擇

$(document).on("change", "#IsCurrentlySmokingrightNej", function() { 
         //---^---here 

同樣適用於其他的前面錯過# ..

+0

ARR如此令人沮喪的是小東西解決吧謝謝 –

+0

!!!! ).. welcome ....開心編碼.. – bipen

1

你需要給#或。如果它是id然後#和如果類名稱然後。

$(document).on("change", "#IsCurrentlySmokingrightNej", function() { 
          ^^^ # here as it is id 

還有一兩件事:你爲什麼要使用,即使你不使用它的任何地方這條線

var elem = document.getElementById("IsCurrentlySmokingrightNej"); 

+0

我之前實際使用過這個代碼,但是我已經修改了幾次代碼,所以現在只能用代碼的方式了。謝謝! –

+0

@DanielGustafsson好的,只是爲了瞭解正確的問題。以及沒有問題。 –