简体   繁体   English

如何在javascript中使用自动增量ID?

[英]how to use auto increment id in javascript?

<script>
function show(id){
$("button").click(function(){
    $("p") .toggle();
});
});
</script>
 <?php 
    $bid=0;
    $bid=0;
    include 'model.php';
    $db=new database;
    $r=$db->msghead($admno);
    while($row= mysql_fetch_array($r))
    {
        $id=$row[0];
        $title=$row[1];
        $msg=$row[2];
        $date=$row[3];
        $sender=$row[4];
        $tit_status=$row["title_status"];
        $bid=$bid+1;
        $pid=$pid+1;
?>
<button style="width:80%;height:35px;" onclick="MessageDetailsById(<?php echo $id;?>)"   >


    <?php   
        if($tit_status=="1"){
    ?>      
        <i class="fa fa-plus-circle" aria-hidden="true" style="width:20px;float:left;"></i>  <i class="fa fa-envelope-open-o" aria-hidden="true"></i>&nbsp;<span id="active" class="sidebar-title" style="color:red;"><?php echo $title; ?></span> &nbsp;&nbsp;<?php echo $date;?>
<?php }else{?>      
        <i class="fa fa-plus-circle" aria-hidden="true" style="width:20px;float:left;"></i> <i class="fa fa-envelope-o" aria-hidden="true"></i>&nbsp;<span  id="active<?php echo $tit_status;?>" class="sidebar-title"><?php echo $title;  ?></span>    &nbsp;&nbsp;<?php echo $date;?>
<?php }?>
        </a></button>

<p style="display:none;" id="<?php echo $id; ?>">   <?php echo $msg; ?> 

      <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
        Reply
      </a>

</p>
<?php } ?>

so many buttons generated according to database.. but when i click single button all paragraph get open but i want it open only the paragraph related to its button.. i dont know which id will be used and how ??? 如此多的按钮根据数据库生成..但当我点击单个按钮所有段落打开但我希望它只打开与其按钮相关的段落..我不知道将使用哪个ID和如何???

What you want(toggle current and hide all at the same time) for that you have to change your structure and do like below:- 你想要的是什么(切换当前和隐藏所有内容),你必须改变你的结构,如下所示: -

Example with your structure (you have to put your dynamic data carefully):- 您的结构示例(您必须仔细地放置您的动态数据): -

 $('.item p').hide(); $('.item button').click(function(e){ e.preventDefault(); var $this = $(this).parent().find('p'); $(".item p").not($this).hide(); $this.toggle(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="item"> <!-- this is needed otherwise not possible --> <button>Button</button> <p>span1</p> <br> </div> <div class="item"> <button>Button</button> <p>span2</p><br> </div> <div class="item"> <button>Button</button> <p>span3</p><br> </div> <div class="item"> <button>Button</button> <p>span4</p><br> </div> 

Reference taken:- http://jsfiddle.net/BGSyS/3/ 参考文献: - http://jsfiddle.net/BGSyS/3/

you are assigning id to p 你正在为p分配id

<p style="display:none;" id="<?php echo $id; ?>">

so just change your jquery code to this : 所以只需将你的jquery代码更改为:

  function MessageDetailsById(id){
    $("button").click(function(){
      $("#"+id) .toggle();
    });
  }

Try something like: 尝试类似的东西:

<button ... data-id="<?php echo $id; ?>" ...>...</button>
<p ... data-id="<?php echo $id; ?>" ...>...</p>

And then On click do something like: 然后点击执行以下操作:

$('button').click(function(){
    $('p').find("[data-id='" + $(this).data('id') + "']").toggle();
});

I have not tested but I do use this case with dynamic elements in my programs. 我没有测试,但我在程序中使用动态元素。

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

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