-2

可能重复:
在 AJAX 请求之前使用 JavaScript 获取日期

我有一个这样的 Ajax 请求:

function ajaxrequest(str){

    var aircraft = $("#resultdiv");
        aircraft.load("./other_file.php?icao="+str, function(){
        });
    }

如果我将 Javascript 放在 other_file.php 中不起作用。我不知道为什么?但是如果直接使用 javascript 代码就很有趣!但我需要这个功能。如何使用 Javascript 将函数更改为结果?

例如:

如果 other_file.php 有这个:

<script>
function example(){

        var id = document.getElementById('id').value;
                $("div2").text(id);
    }
</script>

这不起作用,因为它需要获取 var id,但如果元素的值是 2365。获取它的 javascript 代码document.getElementById('id').value;不起作用,因为 javascript 不起作用。但是如果我直接这样做而不是通过函数 ajaxrequest 一切正常。

这是 other_file.php 的真正包含

<script>
    $(function() {
        $("#editaircraft")
            .button()
            .click(function editForm() {
        });
    });

    function editForm(){

        var icao = document.getElementById('icao').value;
        var name = document.getElementById('name').value;
        var weightempty = document.getElementById('weightempty').value;
        var weightfull = document.getElementById('weightfull').value;
        var cargofull = document.getElementById('cargofull').value;
        var cruisespeed = document.getElementById('cruisespeed').value;
        var range = document.getElementById('range').value;
        var price = document.getElementById('price').value;
        var firstclassseats = document.getElementById('firstclassseats').value;
        var businessclassseats = document.getElementById('businessclassseats').value;
        var economyclassseats = document.getElementById('economyclassseats').value;
        ajax.open("POST","edit_aircraft_process.php",true);
        ajax.onreadystatechange=function(){
            if(ajax.readyState==4)
            {
            refreshTable(function(){$("#loadingdialog").dialog('close');});

            refreshTable(function(){$("#result").fadeIn(); document.getElementById("result").innerHTML=ajax.responseText;});
            setTimeout(function() { $("#result").fadeOut() }, 5000);
            }
        }
    ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax.send("icao="+icao+"&name="+name+"&weightempty="+weightempty+"&weightfull="+weightfull+"&cargofull="+cargofull+"&cruisespeed="+cruisespeed+"&range="+range+"&price="+price+"&firstclassseats="+firstclassseats+"&businessclassseats="+businessclassseats+"&economyclassseats="+economyclassseats);
    $("#editaircraftdialog").dialog('close');
    $("#loadingdialog").dialog('open');
    }
</script>

<?php
require_once ('../../config.php');
$icao = $_REQUEST["icao"];

$query = "SELECT * FROM aircrafts WHERE ICAO = '$icao'";

$result = mysql_query($query);
if (!$result)
{
die (mysql_error());
}

?>
<form action="javascript:editForm();" method="post" enctype="application/x-www-form-urlencoded"> 
<?php
echo '<table border="0">';
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){

echo '<tr><td class="forms">ICAO:</td><td><input type="text" id="icao" name="icao" size="30" value=';
echo $row["ICAO"] . "></td></tr>";

echo '<tr><td class="forms">Name:</td><td><input type="text" id="name" name="name" size="30" value=';
echo $row["Name"] . "></td></tr>";

echo '<tr><td class="forms">Weight Empty:</td><td><input type="text" id="weightempty" name="weightempty" size="30" value=';
echo $row["WeightEmpty"] . "></td></tr>";

echo '<tr><td class="forms">Weight Full:</td><td><input type="text" id="weightfull" name="weightfull" size="30" value=';
echo $row["WeightFull"] . "></td></tr>";

echo '<tr><td class="forms">Cargo Full:</td><td><input type="text" id="cargofull" name="cargofull" size="30" value=';
echo $row["CargoFull"] . "></td></tr>";

echo '<tr><td class="forms">Cruise Speed:</td><td><input type="text" id="cruisespeed" name="cruisespeed" size="30" value=';
echo $row["CruiseSpeed"] . "></td></tr>";

echo '<tr><td class="forms">Range:</td><td><input type="text" id="range" name="range" size="30" value=';
echo $row["Range"] . "></td></tr>";

echo '<tr><td class="forms">Price:</td><td><input type="text" id="price" name="price" size="30" value=';
echo $row["Price"] . "></td></tr>";

if($row["FirstClassSeats"] != NULL && $row["FirstClassSeats"] != 0){
echo '<tr><td class="forms">First Class Seats:</td><td><input type="text" id="firstclassseats" name="firstclassseats" size="30" value=';
echo $row["FirstClassSeats"] . "></td></tr>";}

if($row["BusinessClassSeats"] != NULL && $row["BusinessClassSeats"] != 0){
echo '<tr><td class="forms">Business Class Seats:</td><td><input type="text" id="businessclassseats" name="businessclassseats" size="30" value=';
echo $row["BusinessClassSeats"] . "></td></tr>";}

if($row["EconomyClassSeats"] != NULL && $row["EconomyClassSeats"] != 0){
echo '<tr><td class="forms">Economy Class Seats:</td><td><input type="text" id="economyclassseats" name="economyclassseats" size="30" value=';
echo $row["EconomyClassSeats"] . "></td></tr>";}

echo '<tr><td><input id="editaircraft" type="submit" value="Edit Aircraft"></td></tr>';

}
echo "</table>";
?>

</form>

vars 的获取是不确定的!

4

1 回答 1

0
var aircraft = $("#resultdiv");

function ajaxrequest(str){
    $.get("other_file.php?icao="+str, function(result){
          aircraft.append(result);
    });
}

other_file.php

...

    <script>
            $("#editaircraft")
                .find('button') /* ??? */
                .click(editForm);

        function editForm(){

            var icao = document.getElementById('icao').value;
            var name = document.getElementById('name').value;
            var weightempty = document.getElementById('weightempty').value;
            var weightfull = document.getElementById('weightfull').value;
            var cargofull = document.getElementById('cargofull').value;
            var cruisespeed = document.getElementById('cruisespeed').value;
            var range = document.getElementById('range').value;
            var price = document.getElementById('price').value;
            var firstclassseats = document.getElementById('firstclassseats').value;
            var businessclassseats = document.getElementById('businessclassseats').value;
            var economyclassseats = document.getElementById('economyclassseats').value;
            ajax.open("POST","edit_aircraft_process.php",true);
            ajax.onreadystatechange=function(){
                if(ajax.readyState==4)
                {
                refreshTable(function(){$("#loadingdialog").dialog('close');});

                refreshTable(function(){$("#result").fadeIn(); document.getElementById("result").innerHTML=ajax.responseText;});
                setTimeout(function() { $("#result").fadeOut() }, 5000);
                }
            }
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send("icao="+icao+"&name="+name+"&weightempty="+weightempty+"&weightfull="+weightfull+"&cargofull="+cargofull+"&cruisespeed="+cruisespeed+"&range="+range+"&price="+price+"&firstclassseats="+firstclassseats+"&businessclassseats="+businessclassseats+"&economyclassseats="+economyclassseats);
        $("#editaircraftdialog").dialog('close');
        $("#loadingdialog").dialog('open');
        }
    </script>
于 2013-01-19T19:21:18.510 回答