childNodes [loop] .text返回" undefined"在Chrome中

时间:2014-07-29 07:56:58

标签: javascript jquery ajax jsp google-chrome

我有一个jsp,其中在用户从“业务”下拉列表中选择一个企业后,加载了子企业的下拉列表。该代码在IE 7中运行良好,但在Chrome中可能是地狱,可能是更高版本的IE。代码如下(对不起这么长的代码)

JSP

<fieldset> <table>
<tr>
<td class="tdlabel">
Business
</td><td>
     <select name="business" onchange="javascript:fetchSubBusiness_Ajax(this.form.business);">
       <option value="">
       --select--
       </option>
<logic:iterate id="elementz" name="visaFields" property="gebiz">
       <option value="<bean:write name="elementz"></bean:write>">
       <bean:write name="elementz"></bean:write>
       </option>
</logic:iterate>
      </select>
 </td>
 <td class="tdlabel">
 Sub-Business
 </td><td>
      <select name="subbusiness">
      <option value="">
      --select--
      </option>
      </select>
  </td>
  </tr>

的Javascript

function fetchSubBusiness_Ajax(val){
 if(val != -1)
{

            var myindex  = val.selectedIndex;
            var selValue = val.options[myindex].value;


            business = selValue;
    var url = "visa_fetchsubbusiness.do?business=" +business;

    if(window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
        try
        {
            req.open("GET",url,true);
        }
        catch(e)
        {
            alert(e);
        }

        req.onreadystatechange = processfetchsubbusiness;
        req.send(null);
    }
    else if(window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req)
        {

            req.open("GET", url, true);
            req.onreadystatechange = processfetchsubbusiness;
            req.send(null);
        }
    }
}



}

function processfetchsubbusiness ()
{
    if (req.readyState == 4)
    {
        if (req.status == 200)
{
      try{
        var responseXML = req.responseXML;
        var parents = responseXML.getElementsByTagName("subbusiness")[0];
        alert("parents " +parents.text);
        var child;
        var subbussiness;

while (document.forms[0].subbusiness.length> 1) {
document.forms[0].subbusiness.remove(1);
}

document.forms[0].subbusiness.options[0]=new Option("--select--","--select--");
        for (var loop = 0; loop < parents.childNodes.length; loop++)
        {
                child = parents.childNodes[loop];
                subbussiness = child.text;
                alert("subbussiness = " +subbussiness);



                                    var opt = new Option(subbussiness,subbussiness);
                                    document.forms[0].subbusiness.options[loop+1] = opt;

                }
                }catch(e){

                alert("Exception in Fetching the Sub Business for the selected Business");


             }
    }
}
}

在警报

alert("subbussiness = " +subbussiness);

我得到subbussiness = undefined 。对于第二个选择菜单中的选项也是如此。

有人可以告诉我为什么我在Chrome中获得 undefined 值吗?

1 个答案:

答案 0 :(得分:0)

因为这被标记为jquery你应该能够像这样工作:

而不是

subbussiness = child.text;

使用:

subbussiness = $(child).text();

这将创建xml元素“child”并访问文本值