Javascript 使用jQuery更改验证(html5)

Javascript 使用jQuery更改验证(html5),javascript,jquery,html,forms,validation,Javascript,Jquery,Html,Forms,Validation,我需要一个html5表单更改验证代码。 在我的confirm password中,我有一个onKeyUp,当用户更改confirm password字段时,更改背景图像,现在完成代码 html: <input class="txtNewPassword" type="password" name="password1" id="txtConfirmPassword" required title="enter password"> 如何将jquery验证添加到onKeyUp?这里工

我需要一个html5表单更改验证代码。 在我的
confirm password
中,我有一个
onKeyUp
,当用户更改
confirm password
字段时,更改背景图像,现在完成代码

html

<input class="txtNewPassword" type="password" name="password1" id="txtConfirmPassword" required title="enter password">

如何将jquery验证添加到
onKeyUp

这里工作正常:您想要这样的东西吗?(我删除了背景,以显示它的工作原理)我尝试这个代码的朋友,但它不是我需要的东西!我需要一个像{$(“#txt”).css(“背景图像”,url(images/root/valid.png)”);}这样的代码,只要一行代码!无关的建议:典型的验证应该在元素上切换一个类,比如
invalid
,并保留实际的样式(
背景图像
)现在,您正在混合显示代码和业务逻辑代码。
$(document).ready(function () {
   $("#txtConfirmPassword").keyup(checkPasswordMatch);
});

function checkPasswordMatch() {
var password = $("#txtNewPassword").val();
var confirmPassword = $("#txtConfirmPassword").val();

if (password != confirmPassword)
{
    $('#txtNewPassword').css("background-image", "url(images/root/invalid.png)");  
    $('#txtConfirmPassword').css("background-image", "url(images/root/invalid.png)");  
}
else
{
    $('#txtNewPassword').css("background-image", "url(images/root/valid.png)");  
    $('#txtConfirmPassword').css("background-image", "url(images/root/valid.png)");  
}
}