当用户在输入框中输入值时,我有一个使用onchange事件运行的javascript。
脚本是
<script type="text/javascript">
function showUser(userNumber, str)
{
if (str=="")
{
document.getElementById("txtHint" + userNumber).innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById("txtHint" + userNumber).innerHTML=xmlhttp.responseText;
var responseText = xmlhttp.responseText;
var description = responseText;
var warehouse = "";
var sellingUnits = "";
if (responseText.indexOf("NOT A VALID") == -1)
{
description = responseText.substring(12, responseText.indexOf(",Warehouse:"));
warehouse = responseText.substring(responseText.indexOf(",Warehouse:")+11, responseText.indexOf(",SellingUnits:"));
sellingUnits = responseText.substring(responseText.indexOf(",SellingUnits:")+14);
}
document.getElementById("whse" + userNumber).innerHTML = warehouse;
document.getElementById("txtHint" + userNumber).innerHTML = description;
document.getElementById("su" + userNumber).innerHTML = sellingUnits;
}
}
xmlhttp.open("GET","getdata1.php?q="+str,true);
xmlhttp.send();
}
</script>
,表格是
<form name="orderform" id="orderform" action="newsale.php" method="post">
<table border=1>
<tr>
<td>Product Code</td>
<td>Description</td>
<td>Warehouse</td>
<td>Selling Units</td>
</tr>
<tr id="r1">
<td width=100>
<input size=10 type=number id=sku1 name=sku1 value="1182" onchange="showUser(1, this.value)">
</td>
<td width=280>
<div align="left" id="txtHint1"> </div>
</td>
<td width=100>
<div align="left" id="whse1"> </div>
</td>
<td width=100>
<div align="left" id="su1"> </div>
</td>
</tr>
<tr id="r2">
<td>
<input size=10 type=number id=sku2 name=sku2 value="1183" onchange="showUser(2, this.value)">
</td>
<td>
<div align="left" id="txtHint2"> </div>
</td>
<td>
<div align="left" id="whse2"> </div>
</td>
<td width=100>
<div align="left" id="su2"> </div>
</td>
</tr>
<tr id="r3">
<td>
<input size=10 type=number id=sku3 name=sku3 value="1174" onchange="showUser(3, this.value)">
</td>
<td>
<div align="left" id="txtHint3"> </div>
</td>
<td>
<div align="left" id="whse3"> </div>
</td>
<td width=100>
<div align="left" id="su3"> </div>
</td>
</tr>
<tr id="r4">
<td>
<input size=10 type=number id=sku4 name=sku4 value="1046" onchange="showUser(4, this.value)">
</td>
<td>
<div align="left" id="txtHint4"> </div>
</td>
<td>
<div align="left" id="whse4"> </div>
</td>
<td width=100>
<div align="left" id="su4"> </div>
</td>
</tr>
</table>
</form>
如上所述,该脚本完美适用于onchange事件。我已经为每个输入框定义了值,当页面加载时我希望为这些值调用和执行脚本。一旦用户更改了值,就必须重新运行新值。
我尝试将以下脚本放在页面的标题部分,但它在适用的行中不一致。我的技能不是很好,所以我觉得我错过了一些小事。
<script type="text/javascript">
window.onload = function() {
showUser(1, document.getElementById("sku1").value);
showUser(2, document.getElementById("sku2").value);
showUser(3, document.getElementById("sku3").value);
showUser(4, document.getElementById("sku4").value);
}
</script>
任何建议都会一如既往地受到赞赏。 感谢致敬, 莱恩
答案 0 :(得分:1)
将脚本放在底部,就在关闭正文标记之前。 您还可以考虑使用jQuery,然后使用$(document).ready
编辑:我的意思是你问题中的最后一个脚本
答案 1 :(得分:1)
远景,请在现有代码中尝试此操作。将xmlhttp声明为函数的局部变量。
<script type="text/javascript">
function showUser(userNumber, str)
{
var xmlhttp = null; // line aded.
if (str=="")
{
document.getElementById("txtHint" + userNumber).innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//document.getElementById("txtHint" + userNumber).innerHTML=xmlhttp.responseText;
var responseText = xmlhttp.responseText;
var description = responseText;
var warehouse = "";
var sellingUnits = "";
if (responseText.indexOf("NOT A VALID") == -1)
{
description = responseText.substring(12, responseText.indexOf(",Warehouse:"));
warehouse = responseText.substring(responseText.indexOf(",Warehouse:")+11, responseText.indexOf(",SellingUnits:"));
sellingUnits = responseText.substring(responseText.indexOf(",SellingUnits:")+14);
}
document.getElementById("whse" + userNumber).innerHTML = warehouse;
document.getElementById("txtHint" + userNumber).innerHTML = description;
document.getElementById("su" + userNumber).innerHTML = sellingUnits;
}
}
xmlhttp.open("GET","getdata1.php?q="+str,true);
xmlhttp.send();
}
</script>