简体   繁体   English

javascript函数从jsp页面返回“ undefine”

[英]javascript function return “undefine” from jsp page

i have a jsp page and call a JS function which is in some abc.js file from this JSP page. 我有一个jsp页面,并从此JSP页面调用某个abc.js文件中的JS函数。 i have included this js file to jsp page. 我已经将此js文件包含到jsp页面中。

JSP JavaScript Code:- JSP JavaScript代码:-

function doFinish(tableId, col, field)
{
   var oldselectedCells = "";
   var selItemHandle = "";
   var selRightItemHandle = "";
   var left = -1;
   var right = -1;
   // Get the table (tBody) section
   var tBody = document.getElementById(tableId);
   // get field in which selected columns are stored
   var selectedCellsFld = document.getElementById(tableId + datatableSelectedCells);

   selectedCellsFld.value = oldselectedCells;
   for (var r = 0; r < tBody.rows.length; r++)
   {
      var row = tBody.rows[r];
      if (row.cells[col].childNodes[0].checked == true)
      {
         selectedCellsFld.value = oldselectedCells +
                                 row.cells[col].childNodes[0].id;
         selItemHandle = row.cells[col].childNodes[0].value

         oldselectedCells =  selectedCellsFld.value + datatableOnLoadDivider;

         left = selItemHandle.indexOf("=");
         right = selItemHandle.length;
         selRightItemHandle = selItemHandle.substring(left+1,right);
         var index=getColumnIndex(tBody,"Name");
         if(index!=null)
         {
            if(field == 1)
            {
               window.opener.document.TemplateForm.eds_asbactionscfg_item_handle_child_physpart.value = selRightItemHandle;
               window.opener.document.TemplateForm.ChildPhysicalPart.value = row.cells[index].childNodes[0].innerHTML;
            }
            else if (field == 2)
            {
               window.opener.document.TemplateForm.eds_asbactionscfg_dev_doc_item_handle_name.value = selRightItemHandle;
               window.opener.document.TemplateForm.DeviationObject.value = row.cells[index].childNodes[0].innerHTML;
            }
            else if (field == 3)
            {
               window.opener.document.TemplateForm.eds_asbactionscfg_dev_doc_item_handle_name.value = selRightItemHandle;
               window.opener.document.TemplateForm.DeviationObject.value = row.cells[index].childNodes[0].innerHTML;
            }
         }

      }
   }

   window.close();
}

JS Code:- JS代码:-

function getColumnIndex(tBody,columnName)
 {
    var cells = tBody.parentNode.getElementsByTagName('th');     
    for (var i=0;i<cells.length; i++)
    {
       if(cells[i].hasChildNodes())
       {
          if(cells[i].childNodes[0].innerHTML.replace(/(\r\n|\n|\r)/gm ,"").trim() == columnName)
          {
             return i;
          }
       }
    }
 }

i had debug this code with firebug & calling getColumnIndex(tBody,columnName) function works fine but when it return to caller the var index=getColumnIndex(tBody,"Name"); 我已经用firebug调试了此代码,并调用getColumnIndex(tBody,columnName)函数可以正常工作,但是当它返回调用方时, var index=getColumnIndex(tBody,"Name"); the index value is "undefine". 索引值为“未定义”。

suggest some solution. 建议一些解决方案。

getColumnIndex(tBody,columnName) function works fine. getColumnIndex(tBody,columnName)函数可以正常工作。 as if it matches this if condition 好像它符合这个条件

 if(cells[i].childNodes[0].innerHTML.replace(/(\r\n|\n|\r)/gm ,"").trim() == columnName)
          {
             return i;
          }

so that it returns something. 这样它就返回一些东西。

but when you replace this var index=getColumnIndex(tBody,"Name"); 但是当您替换此var index=getColumnIndex(tBody,"Name"); so that coulmnName would be "Name" in String. 因此,coulmnName在字符串中将为“名称”。 And it doesn't match with any columnName so that your condition going to be wrong and function doesn't return anything. 而且它与任何columnName不匹配,因此您的条件将是错误的,并且函数将不返回任何内容。

var index=getColumnIndex(tBody,"Name"); the index value is "undefine". 索引值为“未定义”。

suggestion is put some else condition on that and return some error message like this : 建议在其上放置其他条件,并返回如下错误消息:

 if(cells[i].childNodes[0].innerHTML.replace(/(\r\n|\n|\r)/gm ,"").trim() == columnName)
          {
             return i;
          } else{
       // put some error message
       // return null   
}

i had debug this code with firebug & calling getColumnIndex(tBody,columnName) function works fine 我已经用firebug调试了此代码并调用getColumnIndex(tBody,columnName)函数工作正常

From this, I'm assuming that there isn't anything wrong with the implementation of your getColumnIndex function, so your issue with getting an undefined value must have to do with when this function is returning a value. 从此,我假设没有什么不对您的实现getColumnIndex功能,所以用得到一个您的问题undefined值必须这个函数返回值与做。

but when it return to caller the var index=getColumnIndex(tBody,"Name"); 但当返回调用方时,var index = getColumnIndex(tBody,“ Name”); the index value is "undefine". 索引值为“未定义”。

This leads me to assume that your tBody variable is not being set correctly, given that the "function works fine". 考虑到“功能正常”,这使我假设您的tBody变量未正确设置。

I'm assuming there is a case in your code where the conditions of your getColumnIndex function is not met. 我假设您的代码中有一种情况,其中不满足您的getColumnIndex函数的条件。

function getColumnIndex(tBody,columnName)
 {
    var cells = tBody.parentNode.getElementsByTagName('th');     
    for (var i=0;i<cells.length; i++)
    {
       if(cells[i].hasChildNodes())
       {
          if(cells[i].childNodes[0].innerHTML.replace(/(\r\n|\n|\r)/gm ,"").trim() == columnName)
          {
             return i;
          }
       }
    }
    // If your code reaches this point, then the prior conditions have not been met
    // You can choose to do something else here for return false/undefined etc.
    return undefined;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM