gpt4 book ai didi

jquery - 检查文本框字符串并根据字符串内容使用CSS伪类

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:09 25 4
gpt4 key购买 nike

我有一个文本框:

<input type = "text" name = "promo" class = "promobox" id = "promobox" placeholder= "PROMO CODE" />

我想检查该复选框的内容是否是我的促销代码“student”,如果不是,那么我将使用一个伪类,例如,#promobox:badCode{} ,如果匹配,那么我将使用 #promobox:goodCode{}

这是我目前在我的脚本标签中的内容:

$('promobox').keyup(onchange() {
if ($("#promobox").val() != "student") {
$(this).addClass("badCode");
} else {
$(this).addClass("goodCode");
}
});

最佳答案

几个语法问题。您在第一个选择器中缺少 #onchange 应该是 function

在好/坏之间转换时还需要删除相反的类

尝试:

$('#promobox').keyup(function(){
var $this= $(this); // cache $(this);

if( $this.val() != "student"){
$this.addClass("badCode").removeClass('goodcode');
}else{
$this.addClass("goodCode").removeClass('badcode');
}
});

关于jquery - 检查文本框字符串并根据字符串内容使用CSS伪类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29885502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com