在测验应用程序中通过jquery进行文本框验证

时间:2011-09-06 08:34:30

标签: javascript jquery html

我想要填充空白测验样本。我有文本框,这个文本框在测验中将为空白。我想验证此texbox值,如果是真的,我会将文本框的颜色更改为绿色,如果它不会是红色的。你知道这样的jquery样本吗?我想要客户端解决方案。谢谢

2 个答案:

答案 0 :(得分:1)

你的问题不是很清楚。你什么时候想要改变颜色?我将假设您希望它发生在blur事件:

$("#yourTextboxId").blur(function() {
    if(!this.value) {
        $(this).css({backgroundColor: "red"});
    }
    else {
        $(this).css({backgroundColor: "green"});
    }
});

这是working example

答案 1 :(得分:0)

你可以这样做 -

$("#question").blur(function() {
    if ($(this).val() == 'Paris') $(this).css('background-color','green');
    else $(this).css('background-color','red');
});  

这将对blur事件进行验证,以便在文本框失去焦点时触发验证。您需要将验证代码更改为适合您需求的代码。

工作演示 - http://jsfiddle.net/ipr101/eXSrq/

相关问题