dopuz8728 2011-03-23 14:57
浏览 21
已采纳

如何在第一次按键时从数据库中获取数据

i have a text field

<input type="text" class="text" id="txt_sh_vid" onKeyPress="vhc_record()" maxlength="4">

when i pressed key it will call a function i js

function vhc_record()
{
var  data='vhc='+document.getElementById('txt_sh_vid').value;
 loadXMLDoc('ship/vehicle_ship/','div_vhc',data);
   document.getElementById('div_vhc').style.visibility="visible";
}

it will take the text field value and search a result from database

<?php 
$vhc=$_POST['vhc'];
$con=mysql_connect("localhost","root","");
if(!$con)
{
die ("error".mysql_error());
}
$db_found=mysql_select_db("oil",$con);
if($db_found)
{


$qry="select * from vehicle where vehiclenumber like '$vhc%'";
$result=mysql_query($qry);
echo "<table class='select'>
<tr>
<th>id</th>
<th>vehiclenum</th>
<th>capacity</th>
<th>owner</th>

</tr>
";

while($r = mysql_fetch_assoc($result)){

    echo"<tr onclick='vhc_inv()'>";
    echo"<td id='vhcid'>" .$r['vehicleid']."</td>";
echo"<td id='vhcl'>" .$r['vehiclenumber']."</td>";
echo"<td>".$r['capacity']."</td>";
echo"<td>".$r['owner']."</td>";
echo "</tr>";}
echo "</table>";


}


mysql_close($con);

the orignal problem is that it will show the whole table on first keypress and on second key press it will take the firstkeypressed value as a search let suppose when i write n in textfield it will show the whole vehicle table and when i pressed k then it will show only the record having vehicle number starting from n.and when i pressed any other letter then it will show only the record having vehiclenumber=nk *i want it that when i press n in text field it will only show the record of vehicles having vehicle number starting from n*

  • 写回答

1条回答 默认 最新

  • dongqishou7471 2011-03-23 15:08
    关注

    Try to use onKeyUp event instead of onKeyPress

    for example: <input type="text" class="text" id="txt_sh_vid" onKeyUp="vhc_record()" maxlength="4">

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀