Jquery: 在keyup()函数后检查变量是否已定义

4

我正在使用 keyup() 函数和 jQuery 创建注册表单。例如,如果输入正确,我会将其分配给 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" - xAqweRx
typeof txtuname =='undefined' 变更为 undefined === txtuname - xAqweRx
txtuname 放在 script 标签之后。 - xAqweRx
5个回答

1
修改代码。这段代码的主要点是txtuname应该在keyup事件监听器和click监听器的两个范围内都可见。因此,如果有更多元素,请创建验证对象并检查是否设置了所有值并且正确。是的,在您的代码中使用$jQuery
$("document").ready(function(){

    var txtuname = null;

    $("#txtuname").keyup(function(){
        if($("#txtuname").val().length<6){
            jQuery("label[for='txtuname']").text("user name is too short");
        }

        if($("#txtuname").val().length>=6){
            txtuname=$("#txtuname").val();
            jQuery("label[for='txtuname']").text("");
        }
    });

    $("#submitRegistration").click(function(){
        if( txtuname == null || txtuname.length < 6)  ){
            alert("incorrect");
        }
        else{
            alert("correct");
        }

    });
});

更新了变量的检查,使用了@Rhumborl的注释,谢谢


我会在初始化时将其设置为 null,并在无效时将其设置为 null,然后只需检查 if(txtuname === null),而不是重复逻辑。 - Rhumborl

1
将代码替换为以下条件 -
if( typeof txtuname !== 'undefined' && txtuname.length >= 6)  ){
  //proceed further
}else{
   alert('Please correct entries.');
   return false;
}

1
我会将验证逻辑放在一个函数中并调用它,你可以根据你的具体要求更新它并仅执行一次:
function isValidName(field) {
  var myName = field.val().trim();
  // some of this is redundant but just to show possibilities
  var isValid = myName.length && myName.length >= 6 && myName && undefined !== myName && myName != " ";
  var myLabel = $("label[for='" + field.attr('id') + "']");
  if (isValid) {
    myLabel.text("");
  } else {
    myLabel.text("user name is too short");
  }
  return isValid;
}
$("document").ready(function() {
  $("#txtuname").keyup(function() {
    isValidName($(this));
  });
  $("#submitRegistration").click(function() {
    var nameIsValid = isValidName($("#txtuname"));
    if (nameIsValid) {
      alert("valid");
    } else {
      alert("undefined or invalid");
    }
  });
});

我认为这个 undefined !== myName 存在问题,因为每次它都会给我一个答案说用户名太短,如果我从条件中删除 undefined !== myName,那么它就可以工作了。 - Darius92
这应该具有与(typeof myName !== "undefined")相同的效果,除非手动更改了未定义的值,这是可能的。 - Mark Schultheiss

0

你在代码中同时使用了$和jQuery window.jQuery对象。不要同时使用两者,可以通过如下方式进行检查

jQuery("document").ready(function() { jQuery("#txtuname").keyup(function(){ if(jQuery("#txtuname").val().length<6){

    jQuery("label[for='txtuname']").text("user name is too short");
}

if(jQuery("#txtuname").val().length>=6){

var txtuname=jQuery("#txtuname").val();
jQuery("label[for='txtuname']").text("");

}
});

jQuery("#submitRegistration").click(function(){

if(typeof txtuname =='defined'){
    alert("defined");
}
if(typeof txtuname =='undefined'){
    alert("undefined");
}

});
});

或者使用将jQuery替换为$符号。它会起作用。


0

你也可以尝试这种方式......

$("document").ready(function(){

var txtuname;

$("#txtuname").keyup(function(){
    if($("#txtuname").val().length<6){
        jQuery("label[for='txtuname']").text("user name is too short");
    }

    if($("#txtuname").val().length>=6){
        txtuname=$("#txtuname").val();
        jQuery("label[for='txtuname']").text("");
    }
});

$("#submitRegistration").click(function(){
    if(typeof txtuname=="undefined"){
        alert("undefined");
    }
    else{
        alert("defined");
    }
});
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接