当另一个文本框字段具有值时,我需要填充一个文本框。在 JavaScript 中我遇到了一个问题,它不能完美地工作。
首先,让我们看看表单本身的 html 代码:
<div style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
<label>Product Segment</label><label style="color: #F00;">*</label>
</div>
<div class="ui-widget"
style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
<input type="text" name="productsegment" id="productsegment" onkeyup="getagentids();" value="<?php echo $productsegment;?>" />
</div>
<!--Row3 end-->
<!--Row3 -->
<div style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
<label>Product Group</label><label style="color: #F00;">*</label>
</div>
<div
style="width: 145px; height: 30px; float: left; margin-top: 5px; margin-left: 3px;">
<input type="text" name="productgroup" id="productgroup" value="<?php echo $productgroup;?>" />
</div>
脚本代码:
<script type="text/javascript">
var url = "productgroupautofill.php?param=";
function GetHttpObject() {
if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest)
return new XMLHttpRequest();
else {
alert("Your browser does not support AJAX.");
return null;
}
}
function getagentids() {
httpobject = GetHttpObject();
if (httpobject != null) {
var idValue = document.getElementById("productsegment").value;
var myRandom = parseInt(Math.random() * 99999999);
// cache buster
http.open("GET", url + escape(idValue) + "&rand=" + myRandom, true); // from
// here
// it
// wont
// work
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
}
function handleHttpResponse() {
if (http.readyState == 4) {
results = http.responseText;
alert(results);
document.getElementById('productgroup').value = results;
}
}
</script>
productgroupautofill.php 文件内容。
<?php
require_once 'Mysql.php';
$dblink = new Mysql();
$dblink->__construct();
if(strlen($param)>0)
{
$result = mysql_query("select productgroup from productsegmentmaster where productgroup LIKE '$param%'");
if(mysql_num_rows($result)==1)
{
while($myrow = mysql_fetch_array($result))
{
$agentname = $myrow["productgroup"];
$textout .= $agentname;
}
}
else
{
$textout=" , , ,".$param;
}
}
echo $textout;