我被问到是否可以使用某些表单数据并将其显示在HTML中。
我需要将LOADS数据/值相加并乘以RATE数据/值,我认为这会有效,但它在浏览器中没有显示任何内容?
我哪里错了?
我是JavaScript新手,我有HTML / CSS技能。我知道jQuery可能是做这类事情的最好方法,但我不知道。
感谢Kolink昨天告诉我使用textContent或innerText并使用(parseInt或parseFloat)转换为数字,但仍然难过: - (
<html>
<body>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","xmltest.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
var i;
// array loads data from xml//
var data = [xmlDoc.x[1].getElementsByTagName("RATE").innerText, xmlDoc.x[1].getElementsByTagName("LOADS").innerText];
// Loop cycles through array converts to numbers, writes array value //
for (i=0;i<data.length;i++){
parseFloat(data[i]);
document.write("<tr ><td>");
document.write(data[i]);
document.write("</td><tr>");
}
// data array multipled//
var total = (data[0] * data[1])
{
document.write("<tr ><td>");
document.write(total);
document.write("</td><tr>");
}
</script>
</body>
</html>
xml
XML file
<MULTILOAD_TICKET>
<TICKET>
<DATE>12/11/12</DATE>
<ADDRESS>123 FAKE STREET</ADDRESS>
<RATE>300</RATE>
<LOADS>3</LOADS>
<CUSTOMER>Columbia Ales</CUSTOMER>
<ORDERID>BBKHJ1001</ORDERID>
<DRIVER>BOB</DRIVER>
<VEHICAL_REG>UJ78 JHE</VEHICAL_REG>
<MATERIAL>SPOIL</MATERIAL>
<SIG>URL</SIG>
</TICKET>
<TICKET>
<DATE>12/11/12</DATE>
<ADDRESS>123 FAKE STREET</ADDRESS>
<RATE>300</RATE>
<LOADS>6</LOADS>
<CUSTOMER>Columbia Ales</CUSTOMER>
<ORDERID>BBKHJ1001</ORDERID>
<DRIVER>JACK</DRIVER>
<VEHICAL_REG>EU78 JHD</VEHICAL_REG>
<MATERIAL>SPOIL</MATERIAL>
<SIG>URL</SIG>
</TICKET>
<TICKET>
<DATE>15/11/12</DATE>
<ADDRESS>123 FAKE STREET</ADDRESS>
<RATE>300</RATE>
<LOADS>5</LOADS>
<CUSTOMER>Columbia Ales</CUSTOMER>
<ORDERID>BBKHJ1001</ORDERID>
<DRIVER>BOB</DRIVER>
<VEHICAL_REG>UJ78 JHE</VEHICAL_REG>
<MATERIAL>SPOIL</MATERIAL>
<SIG>URL</SIG>
</TICKET>
</MULTILOAD_TICKET>
答案 0 :(得分:0)
在这里,请尝试阅读此https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started。您必须侦听xmlHttpRequest的readystate change事件,并且当它成功时,您将解析XML。