简体   繁体   English

如果 textField 值匹配,则单击时更改按钮图像源

[英]Change button image source when clicked if textField value matches

What the code is supposed to do is, when clicked compare the textField value with the given value and change the source of the clicked button accordingly.代码应该做的是,单击时将 textField 值与给定值进行比较,并相应地更改单击按钮的来源。

It doesn't do that.它不这样做。 I can't seem to find why.我似乎无法找到原因。

<script type="text/javascript">
    
function check(){
    var txt = document.getElementById('t1').value;

    if(txt == "had"){
    document.getElementById('tick').src = "button-check_basic_green.png";}
    else{document.getElementById('tick').src = "button-check_basic_red.png";}   
};

var press = document.getElementById('tick');
press.addEventListener("click", check);

</script>


<p>You <input type="text" id="t1">one job.</p><input type="image" 
src="button-check_basic_blue.png" id="tick"/>
     

Please look at my updated JSFiddle :请看看我更新的JSFiddle

JS: JS:

function check() {
    var txt = document.getElementById('t1').value;

    if (txt === "had") {
        document.getElementById('tick').src = "button-check_basic_green.png";
    } else {
        document.getElementById('tick').src = "button-check_basic_red.png";
    }
};

window.onload = function() {
    var press = document.getElementById('tick');
    press.addEventListener("click", check);
}

EDITED with colored images用彩色图像编辑

EDITED 2 for window.onload编辑 2 用于 window.onload

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM