jquery 通过 KeyUp 运行函数而不是提交
我有这个代码来检查 url 是否是有效的 url。
现在只需按一下按钮即可完成检查。但我想去掉该按钮,而只是在用户在文本框中输入内容或完成输入时进行检查。
我怎样才能实现这个目标?我对 jquery 一点也不熟悉。
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script type="text/javascript">
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});;
</script>
<script>
$(document).ready(function(){
$("#myform").validate({
rules: {
field: {
required: true,
url: true
}
}
});
});
</script>
<style>#field { margin-left: .5em; float: left; }
#field, label { float: left; font-family: Arial, Helvetica, sans-serif; font-size: small; }
br { clear: both; }
input { border: 1px solid black; margin-bottom: .5em; }
input.error { border: 1px solid red; }
label.error {
background: url('http://dev.jquery.com/view/trunk/plugins/validate/demo/images/unchecked.gif') no-repeat;
padding-left: 16px;
margin-left: .3em;
}
label.valid {
background: url('http://dev.jquery.com/view/trunk/plugins/validate/demo/images/checked.gif') no-repeat;
display: block;
width: 16px;
height: 16px;
}
</style>
<form id="myform">
<label for="field">Required, URL: </label>
<input class="left" id="field" name="field" OnKeyUp="$("#myform").validate"/>
<br/>
<input type="submit" value="Validate!" />
</form>
I've got this code which checks if an url is a valid url.
Right now the check is done by the press of a button. But I want to get rid of the button and instead just do the check when the user types something in the textbox or when he's done typing.
How can I achieve this? I am not at all familiar with jquery.
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script type="text/javascript">
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});;
</script>
<script>
$(document).ready(function(){
$("#myform").validate({
rules: {
field: {
required: true,
url: true
}
}
});
});
</script>
<style>#field { margin-left: .5em; float: left; }
#field, label { float: left; font-family: Arial, Helvetica, sans-serif; font-size: small; }
br { clear: both; }
input { border: 1px solid black; margin-bottom: .5em; }
input.error { border: 1px solid red; }
label.error {
background: url('http://dev.jquery.com/view/trunk/plugins/validate/demo/images/unchecked.gif') no-repeat;
padding-left: 16px;
margin-left: .3em;
}
label.valid {
background: url('http://dev.jquery.com/view/trunk/plugins/validate/demo/images/checked.gif') no-repeat;
display: block;
width: 16px;
height: 16px;
}
</style>
<form id="myform">
<label for="field">Required, URL: </label>
<input class="left" id="field" name="field" OnKeyUp="$("#myform").validate"/>
<br/>
<input type="submit" value="Validate!" />
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看下面的代码。我想这就是你所需要的。
Have a look at the code below. I guess this is what you need.
您可以使用本页
http://api 中描述的函数
change()
.jquery.com/change/来捕获您更改输入字段中的文本的时间,然后单击并应用验证器。
You could use the function
change()
that is described in this pagehttp://api.jquery.com/change/
to catch when you change the text in the input field and then click away and afterwards apply your validator.
您应该能够使用验证器上的
element
方法评估单个元素:这将在每个
keyup
事件上验证#field
。这是一个示例: http://jsfiddle.net/bp8wP/
You should be able to evaluate a single element with the
element
method on validator:This will validate
#field
on everykeyup
event.Here's an example: http://jsfiddle.net/bp8wP/