简体   繁体   English

如何从php动态获取值?

[英]How to get values dynamically in js from php?

I want to get dynamic values from php in js in a dynamic manner...Let me elaborate: First I show the image then code: 我想以动态方式从js中的php获取动态值...让我详细说明一下:首先显示图像,然后显示代码: 在此处输入图片说明

<div id="">
 <table border="1" width="820" style="margin-left:15px;">
      <tr>
      <th>Sr #</th>
      <th>Commented Post</th>
      <th>Commenter Name</th>
      <th>Commenter Email</th>
      <th>Comments</th>
      <th>Status</th>
     </tr>

     <?php 

     $values = array();

     while($row= mysqli_fetch_assoc($res)) { 
              $values[] = $row['comment_id']; 

              ?>
          <tr align="center" style="height:50px;">
            <td><?php echo $row['comment_id']; ?></td>
            <td><?php echo $row['post_title']; ?></td>
            <td><?php echo $row['comment_name']; ?></td>
            <td><?php echo $row['comment_email']; ?></td>
            <td><?php echo $row['comment_text']; ?></td>
            <td>
          <?php 
          if($row['status']==0) { ?>
            <div class="status_<?php echo $row['comment_id']; ?>" id="status_<?php echo $row['comment_id']; ?>"><a href="#" style="border-bottom:dotted 2px #0033FF ; text-decoration:none">Unapproved</a></div>    
            <?php  } else { ?>
              Approved
            <?php } ?>

            </td>

          </tr>  

       <?php } 



       ?>


     </table>


     </div>

And Js here's: Js是:

<script type="text/javascript">

 $(document).ready(function(){
     var values = <?php echo json_encode($values); ?>; 

        var id = values[0];

        $('.status_'+id).click(function(){
        $("#status_"+id).html("<b>Test</b>");
         });

     var i = values[1];

        $('.status_'+i).click(function(){
        $("#status_"+i).html("<b>Test</b>");
         });

    });
 </script>

I want to get all comment id's in js dynamically, so that on clicking each row link, it changes to text "Test"...As I have written the js code manually...how we obtain all the comment_id's dynamically in js..I hope you got the idea what I am trying to do... 我想动态获取js中的所有注释id,以便在单击每行链接时,它都变为文本“ Test” ...正如我手动编写js代码...如何在js中动态获取所有comment_id。希望您知道我要做什么...

There's no point in assigning unique ids to each of your elements. 为每个元素分配唯一的ID是没有意义的。 Why not do something like: 为什么不做这样的事情:

<tr>
  <td>...</td>
  <td><a href="#" onclick="toggle(this, <?php echo $id ?>)">Unapproved</td>
</tr>

then something like 然后像

function toggle(node, id) {
   node.parentNode.innerHTML = '<b>test</b>';
   ... do something with "id" value? ...
}

No need for getElementById, assigning a massive pile of repetitive ids, etc.. just a single function call and a bit of DOM tree traversal. 不需要getElementById,分配大量的重复ID等。只需一个函数调用和一点DOM树遍历即可。

You dont appear to actually use the ids. 您似乎并没有实际使用ID。 All you need to do is ascertain the clicked element, which use can do using this 您需要做的就是确定clicked元素,使用this元素可以做到this

html: HTML:

<div class="status"><a href="#" style="border-bottom:dotted 2px #0033FF ; text-decoration:none">Unapproved</a></div> 

js: JS:

 $(document).ready(function(){

    $('.status').click(function(){
        $(this).html("<b>Test</b>");
     });
});

Try to add a class to your status TD, like <td class="status" data-id="<?php echo $row['comment_id'] ?>">// your inital value</td> . 尝试将类添加到状态TD,例如<td class="status" data-id="<?php echo $row['comment_id'] ?>">// your inital value</td>

Using jQuery you can easily listen for events which are attached to a an event listener: 使用jQuery,您可以轻松地侦听附加到事件侦听器的事件:

$(document).on('click', '.status', function() { 
  var id = $(this).attr('data-id');
  $(this).html('<strong>Test</strong>');

  // maybe to something with ajax?
  $.ajax({
    url: 'yourfile.php?call=doSomething&id=' + id,
    type: 'post'
  }).done(function(response) { 
    // console.log(response);
  });
});

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

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