通过单击另一个按钮生成按钮时,单击按钮时更新div [英] Updating div on button click, when the button is generated by clicking on another button

查看:74
本文介绍了通过单击另一个按钮生成按钮时,单击按钮时更新div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击按钮magic1magic2时,div mybox将使用文本,按钮ID和按钮(与magic1magic2不同)进行更新.

On clicking the button magic1 or magic2, the div mybox will be updated with a text, button id and button (different for magic1 and magic2).

单击新生成的按钮后,应在box div中显示新生成的按钮的按钮ID.当我单击新生成的按钮时,box div不会得到更新.这是代码.

After clicking on the newly generated button it should display the button id of the newly generated button in the box div. When I click on the newly generated button, box div is not getting updated. Here is the code.

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

jquerycode.php is the initial file. On clicking the button magic1 or magic2, Ajax will call the page session.php.

jquerycode.php文件

jquerycode.php file

<!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文件

session.php file

 <?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;
    ?>

推荐答案

最好是可以使用 JQuery Live() 用于动态生成的元素:

Best if you can use JQuery Live() for dynamic Generated Elements :

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

因为不赞成使用JQuery Live(),您需要使用 Jquery on()

Beca JQuery Live() is deprecated you need to use JQuery on()

$("#elementid").on("click", function(event){
alert($(this).text());
});

这篇关于通过单击另一个按钮生成按钮时,单击按钮时更新div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆