简体   繁体   English

HTML onclick函数不通过PHP echo调用

[英]HTML onclick function doesn't call through PHP echo

Originally I had the following to produce a menu using PHP: 最初我有以下使用PHP生成菜单:

<ul>
   <?php 
         $items = mysqli_query($con, "SELECT DISTINCT item FROM table");
         while ($row = mysqli_fetch_array($items)) { ?>
             <li><a href='#' onclick='myFunction(<?php echo $row['item'] ?>)'><?php echo $row['item'] ?></a></li><? php 
         } ?>
</ul>

I had this code inside an index.php page surrounded by html. 我在一个被html包围的index.php页面中有这个代码。 The is to produce a menu from items in a database. 是从数据库中的项目生成菜单。 In this form it worked fine and called 'myFunction(string)' which is in a seperate global.js file. 在这种形式下,它工作正常并称为'myFunction(string)',它位于一个单独的global.js文件中。

function myFunction(string) {
$('#container').load('php/getContent.php', {string: string});
}

getContent.php is similar to the php above. getContent.php类似于上面的php。 except it uses a where clause in the query which it extracts from the string. 除了它在查询中使用where子句,它从字符串中提取。


So my problem arose when I went to clean up the code which produces the menu. 所以当我去清理生成菜单的代码时出现了我的问题。 I added the following function to global.js 我将以下函数添加到global.js

$(document).ready(function() {
    $('#menu').load('php/getMenu.php');
});

getMenu.php looks like this: getMenu.php看起来像这样:

$items = mysqli_query($con, "SELECT DISTINCT item FROM table");

echo "<ul class='tab'>";
while ($row = mysqli_fetch_array($items) {
    echo "<li><a href='#' onclick='myFunction(".$row['item'].")'>".$row['item']."</a></li>";
}
echo "</ul>"

This works correctly in producing the menu items as before except the onclick function no longer works. 这可以像以前一样正确生成菜单项,除了onclick功能不再有效。 Placing an anonymous inline onclick function did work however so I figured it may be the $(document.ready() inside the global.js file causing issues. I moved the $(document).ready() back into index.php in a script tag to check, still not working. 放置匿名内联onclick函数确实有效,所以我认为它可能是global.js文件中的$(document.ready()导致问题。我将$(document).ready()移回到index.php中脚本标签要检查,仍然无法正常工作。

I originally had the script tags as the last elements in the body so I moved them up into the header to check if that changed anything but that also didn't work. 我最初将脚本标记作为正文中的最后一个元素,因此我将它们移动到标题中以检查是否更改了任何内容,但这也无效。

the header tag looks as follows: 标头标签如下所示:

<script src='scripts/jquery-3.0.0.js'></script>
<script src='scripts/global.js'></script>
<script>
     $(document).ready(function() {
         $('#menu').load('php/getMenu.php');
     });
</script>

all my php files are in a subfolder called php and all my js files in a subfolder called scripts. 我所有的php文件都在一个名为php的子文件夹中,所有我的js文件都在一个名为scripts的子文件夹中。


So firslty, does it matter if I have the $(document).ready() in a js file with other functions, does that mess with the other functions? 首先,如果我在其他函数的js文件中有$(document).ready(),这有什么关系吗?

Secondly any idea on why the function is no longer being called? 其次是关于为什么函数不再被调用的任何想法?

Thanks in Advance. 提前致谢。

This is a problem I had too. 这也是我遇到的问题。 When you generate elements after the DOM fully loads the onclick wont work anymore. 在DOM完全加载后生成元素时onclick将不再工作。 In my case I used: 在我的情况下我用过:

$('parent of element which is in DOM').delegate('element', 'event', function(){
});

And worked. 并且工作了。

Take a look at jQuery API http://api.jquery.com/delegate/ 看一下jQuery API http://api.jquery.com/delegate/

In your case your php should be like: 在你的情况下你的PHP应该像:

$items = mysqli_query($con, "SELECT DISTINCT item FROM table");

echo "<ul class='tab'>";
while ($row = mysqli_fetch_array($items) {
    echo '<li><a href="#">'.$row['item'].'<input class="sql_input" hidden="hidden" value="'.$row['item'].'"></a></li>';
}
echo '</ul>';

And your Javascript: 而你的Javascript:

function load_file(value){
   $('#container').load('php/getContent.php', {string: value});
}

$('parent of .tab').delegate('.tab li a', 'click', function(){
   var get_value = $(this).find('.sql_input').val();
   load_file(get_value);  
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM