dpd66100 2019-08-13 03:21
浏览 385
已采纳

Jquery函数只执行一次? [重复]

This question already has an answer here:

I'm trying to execute my Jquery so that the table will get updated every time the button is pressed.

test.php

<?php
    require 'mysql.php';
?>
<!-- HTML 5 declaration -->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>FYP LoRa Control Panel</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" media="screen" href="style.css" />
        <!-- jQuery Script -->
        <script
            src="https://code.jquery.com/jquery-3.4.1.min.js"
            integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
            crossorigin="anonymous">
        </script>

        <script>
            // jQuery code
            // jQuery code available after the page has fully loaded
            $(document).ready(function(){
                var id = 0;
                var status = 'temp';

                $(":button").click(function(){
                    id = $(this).prop("id");
                    console.log('button ' + id + ' pressed');

                    if($(this).prop('value') == 'ON'){
                        status = 'OFF';
                    }else{
                        status = 'ON';
                    }

                    // load table with updated values
                    $('#tbody1').load("update-led.php", {
                        id: id,
                        status: status
                    }, function(){
                        console.log('table loaded');
                    });
                });        
            });
        </script>
    </head>
    <body>
        <div class="table">
        <table>
            <thead>
                <tr>
                    <th>No:</th>
                    <th>Name:</th>
                    <th>Status:</th>
                </tr>
            </thead>
            <tbody id='tbody1'>
                <?php
                    getValues();
                ?>
            </tbody>
        </table>
        </div>
    </body>
</html>

update-led.php

<?php
    require 'mysqldb.php';
    require 'mysql.php';

    $id = $_POST['id'];
    $status = $_POST['status'];

    /* start connection */
    $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connection failed: %s
", mysqli_connect_error());
        exit();
    }

    $sql = "UPDATE led_control SET Status='$status' WHERE ID=$id";

    mysqli_query($conn,$sql);

    /* close connection */
    mysqli_close($conn);

    getValues();
?>

mysql.php This will create the table

<?php
    require_once 'mysqldb.php';

    function getValues(){
        /*
        This function retrieves the values from the database
        and store it in an array.
        */
        global $db_host, $db_user, $db_pass, $db_name;
        $data = array();

        /* start connection */
        $conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

        /* check connection */
        if (mysqli_connect_errno()) {
            printf("Connection failed: %s
", mysqli_connect_error());
            exit();
        }

        $sql = 'SELECT * FROM led_control ORDER BY ID';
        if($query = mysqli_query($conn,$sql)){
            while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
                $data[] = $row;

                // Display into html table
                echo "<tr>";
                echo "<td>{$row['ID']}</td>";
                echo "<td>{$row['LED']}</td>";
                echo "<td>
                        <input type='button' id='{$row['ID']}' value='{$row['Status']}' name='{$row['ID']}'>
                    </td>";
                echo "</tr>";
            }

        /* free result set */
        mysqli_free_result($query);
        }

        /* close connection */
        mysqli_close($conn);
    }
?>

For now, it will only get executed once but I would want my function to be executed more than once. I tried to find solutions to my problem but to no avail. I would greatly appreciate any help given :)

</div>
  • 写回答

1条回答 默认 最新

  • dreamy1992 2019-08-13 03:30
    关注

    I guess you have buttons loadded by ajax, first time they are loaded but the second time you get them thru ajax. In case to get event from this buttons you have to go different way like this:

    $(".table #tbody1").on('click', ':button', function(){
                        id = $(this).prop("id");
                        console.log('button ' + id + ' pressed');
    
                        if($(this).prop('value') == 'ON'){
                            status = 'OFF';
                        }else{
                            status = 'ON';
                        }
    
                        // load table with updated values
                        $('#tbody1').load("update-led.php", {
                            id: id,
                            status: status
                        }, function(){
                            console.log('table loaded');
                        });
                    });     
    

    Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器