如何在加载的页面中触发Javascript函数?
所以,我有一个页面,它由两个文本输入字段和一个DIV组成 第一个输入文本字段是用户输入数据的地方,第二个输入文本字段是只读字段。按第一个输入文本字段中的Enter键后,将根据数据库中查询的输入内容在DIV内加载一个单独的页面 问题是我需要将一个值传递给第二个输入字段,该字段也是来自查询的相同数据的只读字段。请帮帮我 示例代码:我制作了一个单独的应用程序,因为这基本上就是我的应用程序应该发生的事情 index.php:如何在加载的页面中触发Javascript函数?,javascript,php,html,Javascript,Php,Html,所以,我有一个页面,它由两个文本输入字段和一个DIV组成 第一个输入文本字段是用户输入数据的地方,第二个输入文本字段是只读字段。按第一个输入文本字段中的Enter键后,将根据数据库中查询的输入内容在DIV内加载一个单独的页面 问题是我需要将一个值传递给第二个输入字段,该字段也是来自查询的相同数据的只读字段。请帮帮我 示例代码:我制作了一个单独的应用程序,因为这基本上就是我的应用程序应该发生的事情 index.php: <!DOCTYPE html> <html>
<!DOCTYPE html>
<html>
<head>
<script>
function loadNow(p,d){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(d).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",p,true);
xmlhttp.send();
}
</script>
</head>
<body>
<input type="text" id="input1" name="input1" placeholder="Input Here"
onkeyup="if ((event.which == 13) || (event.keyCode == 13)) {
loadNow('testnewpage1.php?r='+this.value,'loadHere1'); }" />
<input type="text" id="input2" name="input2" readonly />
<div id="loadHere1">
</div>
</body>
</html>
函数loadNow(p,d){
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(d).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,p,true);
xmlhttp.send();
}
testnewpage1.php
<?php
require('conn.php');
$r=$_GET['r'];
$query = mssql_query("select somefield from sometable where reference='$r'");
$data = mssql_fetch_array($query);
echo "
<script>
input2.value='$data[somefield]';
</script>
";
?>
这将是一个快速而肮脏的解决方案:-
<?php
require('conn.php');
$r=$_GET['r'];
$query = mssql_query("select somefield from sometable where reference='$r'");
$data = mssql_fetch_array($query);
echo "
<script>
document.input2.value='$data[somefield]';
</script>
";
?>
对于javascript,当页面加载时,可以使用document.ready函数并将代码放入其中,当DOM就绪时调用document.ready,可以在加载图像和其他外部内容之前调用
$(document).ready(function() {
/* code here */
});
编辑
对于不起作用的警报函数,您需要在代码中包含Jquery脚本
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
因此,我决定对输出的文本字段使用
,并在新页面中再次显示它
如果有人感兴趣,以下是最终代码:
index.php
<!DOCTYPE html>
<html>
<head>
<script>
function loadNow(p,d){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(d).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",p,true);
xmlhttp.send();
}
</script>
</head>
<body>
<input type="text" id="input1" name="input1" placeholder="Input Here"
onkeyup="if ((event.which == 13) || (event.keyCode == 13)) {
loadNow('testnewpage1.php?r='+this.value,'spaninput'); }" />
<span id="spaninput">
<input type="text" id="input2" name="input2" readonly />
</span>
<div id="loadHere1">
</div>
</body>
</html>
<?php
require('conn.php');
$r=$_GET['r'];
$query = mssql_query("select somefield from sometable where reference='$r'");
$data = mssql_fetch_array($query);
echo "<input type='text' id='input2' name='input2' value='$data[somefield]' readonly />";
?>
函数loadNow(p,d){
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(d).innerHTML=xmlhttp.responseText;
}
}
open(“GET”,p,true);
xmlhttp.send();
}
testnewpage1.php
<!DOCTYPE html>
<html>
<head>
<script>
function loadNow(p,d){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(d).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET",p,true);
xmlhttp.send();
}
</script>
</head>
<body>
<input type="text" id="input1" name="input1" placeholder="Input Here"
onkeyup="if ((event.which == 13) || (event.keyCode == 13)) {
loadNow('testnewpage1.php?r='+this.value,'spaninput'); }" />
<span id="spaninput">
<input type="text" id="input2" name="input2" readonly />
</span>
<div id="loadHere1">
</div>
</body>
</html>
<?php
require('conn.php');
$r=$_GET['r'];
$query = mssql_query("select somefield from sometable where reference='$r'");
$data = mssql_fetch_array($query);
echo "<input type='text' id='input2' name='input2' value='$data[somefield]' readonly />";
?>
我尝试了第一个建议,但它不起作用,即使是alert();它不起作用。第二项建议不适用,因为该表格已采用格式。不能改变,还是不行。你能给我样品吗?如何使用document.ready函数?我试着把我的代码放在那里,它不起作用。你试过把jquery放在你的脚本中吗?document ready通常在页面加载没有问题时运行脚本。我已经将它放在脚本标记中,我的标记中。它不工作。正在加载到只读文本框中的数据是否与您希望查询生成的数据相同?或者查询输出的数据会比需要的多吗?是的,它实际上是同一个查询,但是查询将输出一行或多行,所以我只需要显示行中的同一个值。好吧,我想如何让它符合我的喜好,无论如何,谢谢!