javascript更改文本框的颜色

时间:2013-07-31 12:38:24

标签: javascript jquery

我有这个代码,但它不会工作。问题是什么?我的语法错了还是整个方法都错了?

<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");
        }

});

2 个答案:

答案 0 :(得分:3)

您在ID选择器前面错过了#

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

其余部分也是如此..

答案 1 :(得分:1)

你需要提供#或。如果它是id然后#和if class name那么。

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

还有一件事:

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

即使你没有在任何地方使用它,你为什么要使用这条线。