doucong8553 2016-06-10 10:30
浏览 87
已采纳

JavaScript / JQuery无法在加载了load()的html中工作

What I'm trying to do is, load an input form from another file (lets say that's new_machine.php) to my index.php. In this form I have an input with dropdown which gets options from MySQL database and lists them as anchors. And that works fine. I then want to control user's input with jQuery's .click() method, so when certain anchor is clicked I would be able to get it's text and set it to the value of dropdown button. However that doesn't happen since it seems that jQuery's actions are not triggered. So my question is how do I use jQuery in a partial .html/.php that I load in my index.php. My files look like this:

new_machine.php - the form - I know there is no input for dropdown, I just want to get the text shown in button so I know jQuery works.

<div class="tab-content">

... some text inputs

<label>
    Purchase date<span class="req">*</span>
</label>
<input type="date" required=""  />


<label>
    Machine<span class="req">*</span>
</label>

    <div class="dropdown">
        <button id="chosenGroup" class="dropbtn">Choose machine</button>
        <div class="dropdown-content" id="machineList" >
            <?php
                session_start();
                try {
                    $bdd = new PDO('mysql:host=******:****; dbname=track', '*****', '*****');
                } catch(Exception $e) {
                    exit('Unable to connect to db.');
                }

                $sql = "SELECT * FROM machinegroup WHERE UserID=:uid";
                $q = $bdd->prepare($sql);
                $q->bindParam(':uid', $_SESSION['valid_user']);
                $q->execute();
                while ($row = $q->fetch(PDO::FETCH_ASSOC))
                {
                    echo "<a href=\"#\" class=\"machineGroupClick\">". $row['Name'] ."</a>";
                }   
            ?>
        </div>
    </div>


<script>    
$(document).ready(function(){
    $('.machineGroupClick').click(function(){
        var name = $(this).html();
        $( '#machine-content' ).text('lalal');
        $('#chosenGroup').html(name);
    });
}); 
</script>           


</div>  

index.php

<head>
...
<script src="https://code.jquery.com/jquery.js"></script>
<script src="machines/js/data.js"></script>
...
</head>

<body>
...
<a href="#" id="new-machine">...</a> 

...

<div id="machine-content" class="col-md-9" style="float:right">

...

</body>

data.js (this is jQuery script that handles click and other events in my index.php)

$('#new-machine').click(function(){ 
    $( '#machine-content' ).load('new_machine.php');
}); 

I did also try to put jQuery code from new_machine.php in index.php and in data.js but it never worked. So my question is what is the correct way to use jQuery when loading partial html/php? Am I missing something crucial? Dropdown works fine if the code is all index.php and is not loaded, but I want it to load inside page when user clicks "New machine". Cheers

  • 写回答

1条回答 默认 最新

  • douchen7324 2016-06-10 10:36
    关注

    data.js should look like this:

    $(document).ready(function(){
        $('#machine-content').on('click','.machineGroupClick',function(){//use event delegation here because your html is in the partial and it is loaded after the dom ready
            var name = $(this).html();
            $( '#machine-content' ).text('lalal');
            $('#chosenGroup').html(name);
        });
        $('#new-machine').click(function(){ 
        $( '#machine-content' ).load('new_machine.php');
        }); 
    }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化