JQuery val is null or not an object
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?
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;
精彩评论