[英]How can I check if text input matches a variable?
I have some javascript which is supposed to change the color of an h2 when the user inputs the right text (password). 我有一些JavaScript,当用户输入正确的文本(密码)时,它应该会更改h2的颜色。 But the user can litterally enter anything and it will change the text color.
但是用户可以随意输入任何内容,这将更改文本颜色。 Does anyone know how to fix this?
有谁知道如何解决这一问题? Here is my code:
这是我的代码:
var code = ('user1');
var entry = $('.code').val();
$('.code').change(function(code_check) {
if(code == entry) {
$('.codeText').css('color', 'rgb(247,37,62)');
} else if(code != entry) {
$('.codeText').css('color', 'rgb(61, 216, 97)');
}
});
You need to use the value of the input using this
as the reference 你需要使用使用输入的值,
this
作为参考
$('.code').change(function(){
$(this).css('color', function(){
return $(this).val().trim() == code ? 'rgb(247,37,62)' : 'rgb(61, 216, 97)');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.