javascript 拆分导致“对象不支持此属性或方法”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9207375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:55:59  来源:igfitidea点击:

Split causes "Object doesn't support this property or method" exception

javascriptjquery

提问by Luke

call to spliton a variable causes a "Object doesn't support this property or method"exception and I don't know why. Here's my code:

调用split变量会导致“对象不支持此属性或方法”异常,我不知道为什么。这是我的代码:

function getKontaktPersonen(kontaktSelectBox) {
   var kontaktPersonen = [];
   var id_and_name = kontaktSelectBox.attr('id');
   var id_part = getID_PartFromName(id_and_name);
   var textboxname;
   var selectboxname;
   if (kontaktSelectBox.attr('class') == 'kontaktSelectBox') {

        textboxname = "TextBoxKunde" + id_part;
        selectboxname = "SelectBoxKontaktPerson" + id_part;
    } else if (kontaktSelectBox.attr('class') == 'NewkontaktSelectBox') {
        textboxname = "NewTextBoxKunde" + id_part;
        selectboxname = "NewSelectBoxKontaktPerson" + id_part;
    } else {
        return false;
    }
    var kundeBox = $('#' + textboxname);
    var kundeBoxVal = kundeBox.val();
    if (kundeBoxVal != '' && kundeBoxVal != null) {
     var adr_id = kundeBoxVal.split(';')[1];
      //here comes an ajax call
      //[...]
    }
}

采纳答案by gdoron is supporting Monica

If the selector didn't find any element the valfunction will return undefinedTry this:

如果选择器没有找到任何元素,val函数将返回undefined试试这个:

if (kundeBoxVal) {
     var adr_id = kundeBoxVal.split(';')[1];
 }