2014-10-27 104 views
0

我正在寫一個使用javascript的藥物跟蹤代碼,事情是:我創建了一個textbox,在那個textbox我進一步創建了另一個textbox(子文本框),現在我需要更改該子文本框的顏色。如果條件成立,那麼它應該變綠,如果條件不成立,那麼它應該變成紅色。改變子文本框的顏色

回答

0

試試這個:

的jQuery:

$('#textbox').on('keyup', function(event) { 

    event.preventDefault(); 

    var chk = "medicine" 
    var val = $(this).val(); 

    if(val == chk) 
    { 
     $(this).find(":input").css("background-color", "green"); 
    } 
    else 
    { 
     $(this).find(":input").css("background-color", "red"); 
    } 
} 

,或者如果您知道貴輸入的名稱/ ID:

$('#textbox').on('keyup', function(event) { 

    event.preventDefault(); 

    var chk = "medicine" 
    var val = $(this).val(); 

    if(val == chk) 
    { 
     $(this).find("#subtextbox").css("background-color", "green"); 
    } 
    else 
    { 
     $(this).find("#subtextbox").css("background-color", "red"); 
    } 
} 

或純JS:

$('#textbox').on('keyup', function(event) { 

    event.preventDefault(); 

    var chk = "medicine" 
    var val = $(this).val(); 

    if(val == chk) 
    { 
     document.getElementById('subtextbox').style.background-color = "green"; 
    } 
    else 
    { 
     document.getElementById('subtextbox').style.background-color = "red"; 
    } 
} 
-1

試這個

document.getElementById('your subtext id').style.background-color = "green"; 
0

希望能幫助

<html> 
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
</head> 

<body> 
<p>type the word medicine</p> 
<input type="text" id="textbox" value=""> 
<input type="text" id="subtextbox" value=""> 

<script type="text/javascript"> 

$(document).ready(function(){ 

    $('#textbox').on('keyup', function(event) { 

     event.preventDefault(); 

     // make the condition here 
     // make this dynamic 
     var chk = "medicine" 
     var val = $(this).val(); 

     var sub = $('#subtextbox'); 
     if (chk == val) { 
      sub.css('background-color', 'green'); 
     } else { 
      sub.css('background-color', ''); 
     } 

    }); 

}); 



    </script> 
</body> 
</html> 
+0

這是我真正彪, – sunil 2014-10-27 10:26:14

+0

的document.getElementById( 「bodyRow3Col_1」)style.backgroundColor = 「lightblue」。 document.getElementById(「bodyRow3Col_1InrCol_0」)。style.backgroundColor =「blue」; document.getElementById(「bodyRow3Col_1InrCol_1」)。style.backgroundColor =「red」; document.getElementById(「bodyRow3Col_1InrCol_2」)。style.backgroundColor =「blue」; – sunil 2014-10-27 10:27:26

+0

這就是我真正的想法,我已經在主文本框中創建了一個子文本框,我需要在真或假條件下將該子文本框顏色更改爲紅色和綠色。 – sunil 2014-10-27 10:27:51