【问题标题】:What is the selector for any link inside a table表内任何链接的选择器是什么
【发布时间】:2013-06-24 12:01:35
【问题描述】:

我有一个带有下拉选择器的 php 页面,我使用 jquery、ajax 和 mysql 在选择器更改时生成一个表并放入一个名为 .mytable 的 div 中。表中的一些数据是影响表中数据的 php 脚本的链接,所以我想在不重新加载的情况下刷新表。

我知道我需要通过将事件侦听器附加到表中的链接来执行此操作,但我似乎无法获得正确的选择器来附加侦听器?

任何帮助将不胜感激

用户看到的页面,叫做user.php是:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="css/table_style.css" rel="stylesheet" type="text/css">
</head>



<body>
<form name="form1" method="post" action="">
  <label for="select_data"></label>
  <select name="select_data" id="select_data">
    <option value=1>"Apples"</option>
    <option value=2>"Pears"</option>
    <option value=3>"Bananas"</option>
  </select>
</form>

<div id="mytable">This is where the table goes</div>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="get_table.js"></script>
</body>
</html>

jquery 事件处理程序是:

$(document).ready(function (){
    //listen for select list to change, pass fruit into create_table.php, pass data into mytable div
    $('#select_data').change(function() {
        $.post('create_table.php',{fruit: fruit}, function(data) {
            $('div#mytable').html(data);});     

    });

    $('.fruit_link').change(function() {
        $.post('create_table.php',{fruit: fruit}, function(data) {
            $('div#mytable').html(data);});     

    });

});

创建表并返回 html 代码的事件处理程序调用的脚本是:

<?php

require_once('Connections/dbc.php');
$fruit=$_POST['fruit'];
$sql="SELECT * FROM q_fruit WHERE FruitID =".$fruit;
$rec1=mysqli_query($dbc,$sql);

    echo '<table class="table" align="center">';
    echo '<th class="th" width="80px">Fruit ID</th>';
    echo '<th class="th" width="150px">Fruit Name</th>';
    echo '<th class="th" width="40px">Fruit Action 1</th>';
    echo '<th class="th" width="150px">Fruit Action 2</th>';

    while ($row=mysqli_fetch_array($rec1)) 
        {

            echo '<tr class="tr">';
            echo '<td class="td">'.$row['FruitID']. '</td>';    
            echo '<td class="td">'.$row['FruitName']. '</td>'; 
            echo '<td class="td"><a class="fruit_link" href="fruitaction1.php">'.$row['FruitID']. '</a></td>'; 
            echo '<td class="td"><a class="fruit_link" href="fruitaction2.php">'.$row['FruitID']. '</a></td>'; 
            echo '</tr>';
         }

    echo '</table>';


    mysqli_free_result($rec1);
    mysqli_close($dbc);

?>

因此,每次更改值时,选择事件处理程序都会重现表,但链接处理程序不是,我假设它是因为链接在返回的 html 中,并且尚未出现在用户中java正在监听的文件。有没有办法解决这个问题?

【问题讨论】:

  • $('.mytable table tr td a').click(function(){}) 应该这样做
  • 在此处发布您尝试过的代码。
  • 这可以通过使用 AJAX 轻松完成并检查此stackoverflow.com/questions/5681380/…

标签: jquery selector


【解决方案1】:

我不确定,如果这像$("table a") 一样简单,但看起来是这样的……这将选择所有表中的所有 a(nchor)。

如果你只想在那个表中选择它们,那么$("div.mytable table a"),如果被调用表示一个类,但如果它是一个ID(它确实以点开头)那么@987654323 @。

【讨论】:

  • 我了解这背后的原理,但我遇到的问题是完整的表格 html 被回显到 php 脚本中的 div 中。所以表中的链接没有被监听器捕获。但是脚本中已经存在的链接正在被捕获
  • 目标是当组合框改变值时使用jquery创建表,使用组合中的值作为sql查询的一部分。生成的表格将在数据单元格中包含链接,单击这些链接时应执行后台 php 脚本或函数来更改数据库并刷新表格,而无需离开页面。
  • 您可以在$('div#mytable').html(data); 之后添加监听器,这会将它们附加到新元素上
  • 感谢 balint 的回复,但我不确定您的确切意思,您能说得更具体点吗?
  • 这是对So the links in the table are not being captured by the listener. 的回答如果您在将表格添加到页面(并选择新表格)之后分配 jquery 侦听器,那么它将起作用。
猜你喜欢
  • 2021-09-25
  • 2012-12-22
  • 2012-04-20
  • 2016-12-08
  • 2012-12-28
  • 1970-01-01
  • 2014-05-01
  • 1970-01-01
  • 2014-12-16
相关资源
最近更新 更多