【问题标题】:Let a div get a value from a fetch-array-table row after click on a cell with jquery在使用 jquery 单击单元格后,让 div 从 fetch-array-table 行中获取值
【发布时间】:2014-03-06 12:54:40
【问题描述】:

首先我想说这个问题与jquery replacewith to get data with Ajax after click on a cell有关,如果这违反规则我很抱歉并删除此帖子。

我试图在 MySQL 生成的表中获取单元格的值(使用 fetch_array)。我想单击同一单元格并接收 div 容器中行中第一个单元格的值。

感谢这里的几个条目,我找到了“静态”表的示例。像这样:

<table id="choose-address-table" class="ui-widget ui-widget-content">
  <thead>
    <tr class="ui-widget-header ">
      <th>Name/Nr.</th>
      <th>Street</th>
      <th>Town</th>
      <th>Postcode</th>
      <th>Country</th>
      <th>Options</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td class="nr"><span>50</span>

      </td>
      <td>Some Street 1</td>
      <td>Glasgow</td>
      <td>G0 0XX</td>
      <td>United Kingdom</td>
      <td class="use-address">
        Use
      </td>
    </tr>
    <tr>
      <td id="chip" class="nr">49</td>
      <td>Some Street 2</td>
      <td>Glasgow</td>
      <td>G0 0XX</td>
      <td>United Kingdom</td>
      <td>
          <button type="button" class="use-address">Use</button>
      </td>
    </tr>
  </tbody>
</table>
<br><br>
<div id="glasgow">Hello</div> 

$("#chip").click(function () {
  var scotland = $(this).closest("tr").find(".nr").text();
   
  $("#glasgow").html(scotland);
});

http://jsfiddle.net/FnDvL/231/

工作正常。我将它插入到我的文件 php 中,并从 mysql 获取表数据。

这是我的代码:

<html>
<head>
<script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(".information").click(function () {
  var rodney = $(this).closest("tr").find(".nr").text();
   
  $(".clicked_info").html(rodney);
});
</script>


<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?
$con = mysqli_connect('localhost','root','','boerse');
$sql="SELECT short, name FROM markets";
        $result = mysqli_query($con,$sql);

?>
 



    <div class="markets">

<?
echo "<table>
<thead border='0'>
<tr>
<th>Index</th>
<th>Name</th>
</tr>
</thead>
<tbody border='1'>";
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td class='nr'>" . $row['short'] . "</td>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td class='information'>Use</td>";
  echo "</tr>";
  }
echo "</tbody></table>";
?>
<div class="clicked_info">Hello</div>  
    </div>
<br><br>

            
</body>
</html>

现在我的问题是我可以点击带有类信息的 td,但 div class 'clicked_info' 没有改变。但我确实喜欢小提琴。那么出了什么问题呢?或者问题出在哪里?它必须与 MySQL 问题有关。但为什么呢?

这样我想检查我是否从单元格中获取了内容,并且我可以继续使用它(如我之前的帖子中所见)。

也许任何人都可以提供帮助。如果我在这里违反规则,我再次感到抱歉。

谢谢大家。

【问题讨论】:

  • 将您的事件处理程序 $(".information").click(function () {..... 包装在文档就绪处理程序中

标签: php jquery mysql datatable


【解决方案1】:

您需要将代码包装在 document-ready 处理程序中

$(document).ready(function () {
    $(".information").click(function () {
        var rodney = $(this).closest("tr").find(".nr").text();
        $(".clicked_info").html(rodney);
    });
});

当前,当您的脚本执行时,没有$(".information"),因此事件未正确绑定。

将脚本移至页面底部。喜欢

<script>
$(".information").click(function () {
  var rodney = $(this).closest("tr").find(".nr").text();

  $(".clicked_info").html(rodney);
});
</script>
</body>

你可以使用事件委托

$(document).on("click", ".information",function () {
  var rodney = $(this).closest("tr").find(".nr").text();

  $(".clicked_info").html(rodney);
});

【讨论】:

  • 谢谢,问题已解决。非常感谢,Satpal。
  • @user3088932,很高兴我能帮上忙 :)
  • 你能否最终帮助解决我在主题开头链接的主题?
  • 对。您昨天的帮助非常有帮助,我今天添加了更改后的代码。
  • @user3088932,我会试着让我们切换到那个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2014-04-05
  • 2019-05-25
  • 1970-01-01
  • 2012-10-19
  • 2019-12-31
  • 2012-06-25
相关资源
最近更新 更多