简体   繁体   English

在while循环中处理div

[英]deal with a div within a while loop

I have a while loop shows data items from a database, I want to deal with each item apart, but what I get is just the first div, for each iteration, I wanna take the 'id' of current iteration and deal with it, it's look like 'like' in facebook, when I click on the first 'like' a jquery runs a $.post(..) function and all go fine: treatements done with this id, but when I click on other next 'like's the post methode still sends the first id (checked out by firebug), code: 我有一个while循环,它显示数据库中的数据项,我想分开处理,但我得到的只是第一个div,对于每次迭代,我想获取当前迭代的“ id”并进行处理,在Facebook上看起来像“赞”,当我单击第一个“ $.post(..) ”时,jQuery运行$.post(..)函数,一切正常:使用此ID完成处理,但是当我单击其他下一个“赞”时post methode仍然发送第一个id(由firebug检出),代码:

<script type='text/javascript>
  $("#like").click(function(){
    $.post("done.php",
      {id:$("#id").val()},
      function(data){
        ...
      });
   });
</script>

<body>
$result = myqsl_query("select * from ...");
while($fetch = mysql_fetch_array($result){
..
echo "<input id='id' value='$fetch[0]'>
<a href='...' id='like'>like</a>";
..
</body>

I hope I succeeded to describe the problem! 希望我能成功地描述问题! Any solution with any language will be appreciated! 任何语言的解决方案将不胜感激! Regards. 问候。

If your final output looks something like this: 如果您的最终输出看起来像这样:

<input id='id' value='someValue'>
<a href='someLink' id='like'>like</a>
...
<input id='id' value='someValue'>
<a href='someLink' id='like'>like</a>

Will cause you a problem as an id needs to be unique per dom. 会给您带来麻烦,因为ID必须在每个域中唯一。

You could rework it to use classes: 您可以对其进行改写以使用类:

<input class="likeId" value='someValue'>
<a href='someLink' class='like'>like</a>
...
<input class="likeId" value='someValue'>
<a href='someLink' class='like'>like</a>

And than in your click handler. 而不是在点击处理程序中。

$(".like").click(function(){
    $.post("done.php",
      {id:$(this).prev('.likeId').val()},
      function(data){
        ...
    });
});

Depending if the input value is hidden you could remove it completely and use html data-* attributes and .data() assuming if you can use jQuery greater than 1.4.3. 根据输入值是否被隐藏,您可以将其完全删除,并使用html data-*属性和.data()假设您可以使用大于1.4.3的jQuery。

<a href='someLink' class='like' data-likeId='someValue'>like</a>
<a href='someLink' class='like' data-likeId='someValue'>like</a>

$(".like").click(function(){
    $.post("done.php",
      {id:$(this).data('likeId')},
      function(data){
        ...
    });
});

ID's have to be unique in a HTML file. ID在HTML文件中必须唯一。 If you have multiple elements with the same ID, the browsers only return the first one. 如果您有多个具有相同ID的元素,则浏览器仅返回第一个元素。 Thus, $('#like').click() only attaches the click event handler to the first element with this ID. 因此, $('#like').click()仅将click事件处理程序附加到具有此ID的第一个元素上。

You have to use classes instead: 您必须改为使用类:

<script type='text/javascript>
  $(".like").click(function(){
    $.post("done.php",
      {id:$(this).prev().val()},
      function(data){
        ...
      });
   });
</script>

<body>
<?php
    $result = myqsl_query("select * from ...");
?>
<?php while(($fetch = mysql_fetch_array($result))): ?>
    <input class="something" value="<?php echo $fetch[0]; ?>" />
    <a href="..." class="like">like</a>
<?php endwhile; ?>
</body>

Further notes: 进一步说明:

  • You should not echo HTML, it makes it hard to maintain. 您不应该echo HTML,它会使维护变得困难。 Instead, embed PHP in HTML, like I showed above. 而是将PHP嵌入HTML中,如我上面所示。
  • I also use the alternative syntax for control structures , which is easier to read if you mix HTML and PHP. 我还将控件结构使用替代语法,如果将HTML和PHP混合使用,则更易于阅读。

Maybe you could do something like this: 也许您可以执行以下操作:

<script type='text/javascript>
   $(".like").click(function(){
      $.post("done.php",
         {id:$("#id").prev("input").val())},
         function(data){
         ...
      });
   });
</script>

<body>
    $result = myqsl_query("select * from ...");
    while($fetch = mysql_fetch_array($result){
    ..
    echo "<input name='id' value='$fetch[0]'>
    <a href='...' class='like'>like</a>";
    ..
</body>

You say "first like" so I am assuming that you have many 'like' buttons in the page. 您说“第一次喜欢”,所以我假设您在页面中有很多“喜欢”按钮。

$("#like").click(function(){

but standards would tell you that id's are to be unique. 但是标准会告诉您ID是唯一的。 Maybe a global variable that iterates and appends the iteration to the id such as '#like1', '#like2' etc. 也许是一个全局变量,它将迭代'#like1', '#like2' ID后面,例如'#like1', '#like2'等。

Not sure if this will lead you to your answer but it is something that needs consideration. 不知道这是否会导致您找到答案,但这是需要考虑的问题。

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

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