以下是我的文本框代码
<input id="society_name" onBlur="showsociety(this.value)" />
<input id="societyid" name="society" />
以下是我的javascript,它调用addressdata.php页面...
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
<script>
function showsociety(str)
{
if (window.XMLHttpRequest)
{ xmlhttp=new XMLHttpRequest();}
else
{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var data = JSON.parse(xmlhttp.responseText);
for(var i=0;i<data.length;i++)
{
document.getElementById("societyid").value = data[i].societyid;
}
}
}
xmlhttp.open("GET","addressdata.php?q="+str,true);
xmlhttp.send();
}
</script>
Addressdata.php页面
<?php
require_once('includes/config.php');
$q = $_GET['q'];
$city = $database->getRows("SELECT SM.id AS societyid,SM.society from societymaster SM WHERE SM.society = :society", array(':society'=>"$q"));
$info = array();
foreach($city as $row)
{
$cID = $row['societyid'];
$info[] = array('societyid' => $cID);
}
echo json_encode($info);
?>
我需要在多个文本框中获取id,如上面给出的ex ...在我的表单中。
所以这可以将所有php代码转换为addressdata.php中的函数并仅从javascript调用此函数...
FOR EX - 我需要在函数中创建addressdata.php文件的完整php代码,并在文本框模糊事件中使用下面的javascript调用tis。
答案 0 :(得分:0)
如果我理解正确,你想在你的页面中添加更多的文本输入元素,并能够使用这个元素的整个过程来展示社会。
问题不在于将PHP代码转换为函数(不会带来任何影响)。
你想要的是能够告诉showsociety()函数哪个输入元素应该工作。 在最简单的情况下,您可以在功能中添加其他参数:
showsociety(str, id) {...}
并使用此ID在页面上搜索正确的元素。
[...]
document.getElementById(id).value = data[i].societyid;
[...]
<input id="society_name" onBlur="showsociety(this.value, this.id)" />
它可以做得更好,但我认为通过这样简单的解决方案你不会有太多问题。
希望它有所帮助。