最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery val is null or not an object - Stack Overflow

programmeradmin4浏览0评论

I have a JavaScript function (below) which is inserting from a textbox to a database. I am getting an error:

jquery val is null or not an object in line:

if ($(this).val().indexOf(".") < 0) {

function LoadValues(mode, containers, emptyobj) {
    var ele;
    var obj = emptyobj;
    var idx;
    for(i=0;i<=containers.length-1;i++) {
        switch(mode) {
            case "add":
                ele=$("#" + containers[i]).find("input,textarea,select").not("[type=hidden]");
            break;
            case "update":
                ele=$("#" + containers[i]).find("input,textarea,select,hidden").not("[type=hidden][name^=__]");
            break;
        }
        ele.each(function (x) {
            if ($(this).attr("type") == "checkbox") {
                if ($(this).attr("name") in obj) {
                    switch (typeof (obj[$(this).attr("name")])) {
                        case "boolean":
                            obj[$(this).attr("name")] = ($(this)[0].checked ? true : false);
                            break;
                    }
                }
            }
            else {
                if ($(this).attr("name") in obj) {
                    switch (typeof (obj[$(this).attr("name")])) {

                        case "number":
                            $(this).val((!$(this).val() ? "0" : $(this).val())); // <-- We have changed this line. It used to look like this: $(this).val(($(this).val()=="" ? "0" : $(this).val()));
                            if ($(this).val().indexOf(".") < 0) {
                                obj[$(this).attr("name")] = parseFloat($(this).val());
                            }
                            else {
                                obj[$(this).attr("name")] = parseInt($(this).val());
                            }
                            break;
                            break;
                        case "string":
                            obj[$(this).attr("name")] = $(this).val();
                            break;
                        case "object":
                            obj[$(this).attr("name")] = $(this).val();
                            break;
                        case "boolean":
                            obj[$(this).attr("name")] = ($(this).val() == "true" ? true : false);
                            break;
                    }
                }
            }
        });
    }     
    return obj;
}

What am I doing wrong?

I have a JavaScript function (below) which is inserting from a textbox to a database. I am getting an error:

jquery val is null or not an object in line:

if ($(this).val().indexOf(".") < 0) {

function LoadValues(mode, containers, emptyobj) {
    var ele;
    var obj = emptyobj;
    var idx;
    for(i=0;i<=containers.length-1;i++) {
        switch(mode) {
            case "add":
                ele=$("#" + containers[i]).find("input,textarea,select").not("[type=hidden]");
            break;
            case "update":
                ele=$("#" + containers[i]).find("input,textarea,select,hidden").not("[type=hidden][name^=__]");
            break;
        }
        ele.each(function (x) {
            if ($(this).attr("type") == "checkbox") {
                if ($(this).attr("name") in obj) {
                    switch (typeof (obj[$(this).attr("name")])) {
                        case "boolean":
                            obj[$(this).attr("name")] = ($(this)[0].checked ? true : false);
                            break;
                    }
                }
            }
            else {
                if ($(this).attr("name") in obj) {
                    switch (typeof (obj[$(this).attr("name")])) {

                        case "number":
                            $(this).val((!$(this).val() ? "0" : $(this).val())); // <-- We have changed this line. It used to look like this: $(this).val(($(this).val()=="" ? "0" : $(this).val()));
                            if ($(this).val().indexOf(".") < 0) {
                                obj[$(this).attr("name")] = parseFloat($(this).val());
                            }
                            else {
                                obj[$(this).attr("name")] = parseInt($(this).val());
                            }
                            break;
                            break;
                        case "string":
                            obj[$(this).attr("name")] = $(this).val();
                            break;
                        case "object":
                            obj[$(this).attr("name")] = $(this).val();
                            break;
                        case "boolean":
                            obj[$(this).attr("name")] = ($(this).val() == "true" ? true : false);
                            break;
                    }
                }
            }
        });
    }     
    return obj;
}

What am I doing wrong?

Share Improve this question edited May 18, 2016 at 3:55 Pang 10.1k146 gold badges86 silver badges124 bronze badges asked Mar 8, 2011 at 22:23 RG-3RG-3 6,19819 gold badges72 silver badges127 bronze badges 2
  • 1 Where in the code is this error thrown? – Matt Ball Commented Mar 8, 2011 at 22:25
  • if($(this).val().indexOf(".")<0) { – RG-3 Commented Mar 8, 2011 at 22:38
Add a ment  | 

3 Answers 3

Reset to default 2
if($(this).val().indexOf(".")<0)

You can't call indexOf on null value, so first you have to check if returned value is not empty, then use indexOf.

To make it work, use typeof:

$v=$(this).val();
if(typeof($v)=='string' && $v.indexOf(".")<0)

The error you're having actually spawns from the line above your quoted line. See here:

$(this).val(($(this).val()=="" ? "0" : $(this).val()));

What you're actually doing is the following:

If (the value of this is an empty string) then:
    Set the value to "0"
Else:
    Keep the current value

The problem there is that you're not checking for NULL values. If $(this).val() is NULL, then null will be inserted back into the value of "this", when you'd actually want "0" again, like if you had an empty string.

To fix, replace with the following:

$(this).val((!$(this).val() ? "0" : $(this).val()));

What this does, is instead of just check to see if the value of "this" is an empty string, see if it's a "falsy" value, which is basically anything that is not a populated string. If the contents of $(this).val() is either an empty string, or null, or undefined, then the value "0" will be inserted.

EDIT - Here is what your fixed code should look like:

case "number":
    $(this).val((!$(this).val() ? "0" : $(this).val())); // <-- We have changed this line. It used to look like this: $(this).val(($(this).val()=="" ? "0" : $(this).val()));
    if($(this).val().indexOf(".")<0) {
        obj[$(this).attr("name")]=parseFloat($(this).val());
    }
    else {                                   
        obj[$(this).attr("name")]=parseInt($(this).val());
    }
break;

I haven't copied all of your code here, just the bit that needed changing. Just copy this over the bit that starts with case "number": in your code.

I modified my code:

  if ($(this).val() == null) {

                        }
                        if ($(this).val().indexOf(".") <= 0) {
                            obj[$(this).attr("name")] = parseFloat($(this).val());
                        }
                        else {
                            obj[$(this).attr("name")] = parseInt($(this).val());
                        }
                        break;
发布评论

评论列表(0)

  1. 暂无评论