Javascript/JQuery-val().length'为null或不是对象 [英] Javascript/JQuery - val().length' is null or not an object

查看:310
本文介绍了Javascript/JQuery-val().length'为null或不是对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有错误val().length为null或不是对象",来自代码:

I have the error val().length is null or not an object" from code:

function size(sender, args) {

    var sizeVariable = $("input[id$='txtHello']");
    if (sizeVariable.val().length == 0)
    {
          args.IsValid = false;
     }
}

该错误发生在"If"语句上.我正在尝试检查是否:

The error occurs on the "If" statement. I am trying to check if:

  1. 变量存在
  2. 如果变量中有东西

我认为问题出在点(1).如何检查文本字段是否存在(以希望解决此问题)?

I think the problem lies with point (1). How do I check if the text field exists (to hopefully resolve the issue)?

推荐答案

您可以测试输入字段是否存在:

You can test if the input field exists as such:

if($("input[id$='txtHello']").length > 0) { ... }

如果没有,val()将返回undefined.

您可以立即跳至以下内容:

You could skip immediately to the following:

if(!!$("input[id$='txtHello']").val())

...,因为undefined""都将解析为false

... since both undefined and "" would resolve to false

这篇关于Javascript/JQuery-val().length'为null或不是对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆