0

单击按钮magic1或时,将使用文本、按钮 ID 和按钮(与和不同)更新magic2div 。myboxmagic1magic2

单击新生成的按钮后,它应该在boxdiv 中显示新生成的按钮的按钮 ID。当我单击新生成的按钮时,boxdiv 没有得到更新。这是代码。

jquerycode.php 是初始文件。单击按钮magic1magic2时,Ajax 将调用页面 session.php。

jquerycode.php 文件

<!doctype html>
<?php
$first=$_POST['q'];
echo $first;
?>
<html>
    <head>
        <meta charset="utf-8" />
        <title>My jQuery Ajax test</title>
        <style type="text/css">
            #mybox {
                width: 300px;
                height: 250px;
                border: 1px solid #999;
            }

            #box {
                width: 300px;
                height: 250px;
                border: 1px solid #999;
                position: absolute;
                right:210px;
            }
        </style>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <script>                  

            $(document).ready(function(){ 

    $(".magic_button").click(function() {
        var data = $(this).attr('id');
        //alert (data);
        $.ajax({
            type: "POST",
            url: "session.php",
            data: { 'id': data }
        }).done(function(msg) {
                        $("#mybox").html(msg);          
                    });
    });
});

        </script>
        <script>

        $(".fir").click(function() {
        var dataa = $(this).attr('id');
        alert ("hello");
        $.ajax({
            type: "POST",
            url: "jquerycode.php",
            data: { 'q': dataa }
        }).done(function(msg) {
                        $("#box").html(msg); 
                        return false;
        });
        });

        </script>
    </head>
    <body>
        The following div will be updated after the call:<br />
        <div id="mybox">

        </div>

         <div id="box">

        </div>

        <form name="magic">
   <!-- <label for="name" id="name_label">Name</label>  
    <input type="text" name="name" id="name" size="30" value="" class="text-input" />  
    <label class="error" for="name" id="name_error">This field is required.</label> -->
    <input type="button" class="magic_button" name="magic_button" id="magic_button_1" value="magic1" />
    <input type="button" class="magic_button" name="magic_button" id="magic_button_2" value="magic2" />
</form>

    </body>
</html>

session.php 文件

 <?php
   $id = $_POST['id'];
$id = ucwords(implode(' ',explode('_',$id)));
if($id==="Magic Button 2")
{
    echo "hey its button 2!!";
    ?>

    <input type="button" name="butb" id="second" class="fir" value="second"/>
    <?php
}
else
{
    echo "hey its button 1!!";

    ?>
    <input type="button" name="buta" id="first" class="fir" value="First"/>

    <?php
}
echo $id;
    ?>
4

1 回答 1

0

如果您可以将JQuery Live()用于动态生成元素,则最好:

$("a.offsite").live("click", function(){ alert("Goodbye!"); });                // jQuery 1.3+

因为不推荐使用 JQuery Live() 你需要使用JQuery on()

$("#elementid").on("click", function(event){
alert($(this).text());
});
于 2013-05-04T06:41:02.617 回答