0

我用 Javascript 编写了一个函数,该函数将在页面加载时触发。该功能第一次运行良好。但是如果我在访问其他页面后返回索引页面,它就无法正常工作。它在一定程度上确实可以正常工作,但之后会跳过代码。

以下是我的功能

       <script>function populate() {  

                    //alert("The Flag is "+$('#flag').val());  
                    val =document.getElementById('flag').value;  
                    xml =document.getElementById('xml').value;  
                    alert(xml);  
                    if (val === "M") {  

                        if (window.ActiveXObject) {  
                            doc = new ActiveXObject('Microsoft.XMLDOM');  
                            doc.async = 'false';  
                            doc.loadXML(xml);  
                            alert("ActiveX");  
                        } else {  
                            var parser = new DOMParser();  
                            doc = parser.parseFromString(xml, 'text/xml');  
                        //  alert("DOMparser");  
                        }  
                        alert("Value true");  
                                                   /* upto here function works correctly each time 
                                                    * I have also seen the values of both val and xml are coming correctly 
                                                   */  
                        passportNo = doc  
                                .getElementsByTagName('PASSPORT_NO')[0].childNodes[0].nodeValue;  
                        //alert('passportNo ' + passportNo);  
                        document.getElementById('passportNo').value = passportNo;  
                        pass_type = doc.getElementsByTagName('PASS_TYPE')[0].childNodes[0].nodeValue;  
                        //  alert("Pass_type = " + pass_type);  
                        if (pass_type === "I") {  
                            document.getElementById('in').checked = true;  
                        } else if (pass_type === "O") {  
                            document.getElementById('out').checked = true;  
                        }  
                        jobNo = doc.getElementsByTagName('JOB_NO')[0].childNodes[0].nodeValue;  
                        //alert("jobNo = "+jobNo);  
                        document.getElementById('job_no').value = jobNo;  

                        jobDt = doc.getElementsByTagName('JOB_DT')[0].childNodes[0].nodeValue;  
                        //alert("jobDT "+jobDt);  
                        document.getElementById('DT').value = jobDt;  
                        //Clear xml  

                        nationality =doc.getElementsByTagName('NATIONALITY')[0].childNodes[0].nodeValue;  
                        document.getElementById('nationality2').value = nationality;  
                        element = document.getElementById('nationality');  
                        element.value = nationality;  

                    }  
                } </script> `

这就是我所说的

<body onload="populate()">
<table width="1270" align="center">
    <tr>
        <td width="1010" height="46" colspan="3" align="center"><h1>Currency
                Declaration Form</h1></td>
    </tr>
</table>
<input type="hidden" id="flag" value="<%=code%>" />
<input type="hidden" id="xml" value="<%=xml%>" />
<form name="myForm" action="Entry.do" method="post"
    onsubmit="return validateAll()" class = "autocompleteOff">
    <table width="1042">
        <tr class="heading">

        </tr>
        <tr>
            <td width="256" align="left"><input type="radio" name="inout"
                id="in" value="I" /> <label>INCOMING </label> <input type="radio"
                name="inout" id="out" value="O" /> <label>OUTGOING </label></td>
            <td width="774" align="right"><label>JobNo/DT</label> <input
                type="text" name="job_no" id="job_no" readonly="readonly"
                tabindex="-1" /> <input type="text" name="DT" id="DT"
                readonly="readonly" tabindex="-1" value="<%=Convert.getSysDate()%>" /></td>
        </tr>
    </table>`
4

1 回答 1

1

我在您的 HTML 代码中既看不到passportNoid 也看不到PASSPORT_NO 标签(getElementsByTagName)。和许多其他元素也有同样pass_type的问题。nationality你错过了一些代码吗?或者,也许,这是 PHP 的动态输出(例如),第一次运行后它返回不同的 HTML?

于 2013-08-07T05:34:52.617 回答