简体   繁体   English

如何检查文本输入是否与变量匹配?

[英]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.

相关问题 如何检查输入的值是否匹配并在不匹配时显示一些文本? - How can I check if an input's value matches and display some text if it doesn't? 如何使用JavaScript针对一组标签文本检查所有“ a”元素内部文本是否匹配? - How can I check all “a” elements inner text against a set of labels text for matches using JavaScript? 如何检查正则表达式是否永远匹配? - How can I check if a regex matches forever? 如何检查输入字段的文本是“是”还是“否”? - How can I check if the text of an input field is “yes” or “no”? 如何使用jQuery将输入文本的值附加到变量中? - How can I append the value of an input text into a variable with jQuery? 如何将变量设置为文本输入,然后使其出现在屏幕上 - How can I set a variable to a text input and then make it appear on screen 如何检查 JavaScript 中的字符串是否匹配特殊格式? - How can I check if a string in JavaScript matches a special format? 如何检查我的输入中是否有文字 - How to Check if i have text in my input 如何实时检查输入字段是否与angular.js匹配另一个变量 - How to check in real time if a input field matches another variable with angular.js 如何检查输入是否与 jQuery 的字符串模式匹配 - How to check if on input matches a string pattern with jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM