【发布时间】:2016-05-26 12:00:17
【问题描述】:
我正在使用jquery 函数keyup() 创建注册表单,
例如,如果输入正确,我将其分配给一个 txtuname 变量,然后我按下注册按钮,我需要知道所有表单变量都是正确和定义的。下面的代码不起作用:
<script type="text/javascript">
$("document").ready(function() {
$("#txtuname").keyup(function() {
if ($("#txtuname").val().length < 6) {
jQuery("label[for='txtuname']").text("user name is too short");
}
if ($("#txtuname").val().length >= 6) {
var txtuname = $("#txtuname").val();
jQuery("label[for='txtuname']").text("");
}
});
$("#submitRegistration").click(function() {
if (typeof txtuname == 'defined') {
alert("defined");
}
if (typeof txtuname == 'undefined') {
alert("undefined");
}
});
});
</script>
【问题讨论】:
-
typeof txtuname =='defined'改为typeof txtuname = "string" -
typeof txtuname =='undefined'改为undefined === txtuname -
将
txtuname放在script标签之后
标签: javascript jquery onkeyup