[英]Change div css if span contain text
I have one input and span. 我有一个输入和跨度。 When I type in input value is passed to span. 当我输入时,输入值将传递给span。 Now I want to hide other div/button or something if my span have specific value. 现在,我想隐藏其他div / button或如果我的跨度具有特定值的东西。 My code don't work. 我的代码不起作用。 What's wrong? 怎么了?
$('#input').on('keyup', function(){ $('#status').html($(this).val()); }); $('#status').on('change', function(){ var status = $('#status').html(); if (status == "OK") { $('#test').css("background", "red"); } else { $('#test').css("background", "blue"); } });
#test { width: 50px; height: 50px; background: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="input"> <span id="status"></span> <div id="test"></div>
Instead of checking changes on span element, it is better to do this logic inside of input event listener. 与其检查span元素上的更改,不如在输入事件侦听器内执行此逻辑。 Something like this: 像这样:
$('#input').on('keyup', function(){
var val = this.value;
$('#status').html(val);
if (val == "OK") {
$('#test').css("background", "red");
} else {
$('#test').css("background", "blue");
}
});
Liste the keyup event and change the background 列出keyup事件并更改背景
$('#input').on('keyup', function(){ $('#status').html($(this).val()); console.log($(this).val()); if ($('#status').html() == "OK") { $('#test').css("background", "red"); } else { $('#test').css("background", "blue"); } });
#test { width: 50px; height: 50px; background: red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="input"> <span id="status"></span> <div id="test-container"> <div id="test"></div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.