weixin_33697898 2016-04-12 14:21 采纳率: 0%
浏览 88

设置div唯一ID

I am echoing comments from database and echo looks like this:

echo $status->comment.'

        <textarea id="'.$status->id.'"></textarea>

';

Now i have this code:

<script type="text/javascript">
    $(document).ready(function() {

        $('#'+'<?php echo $status->id;?>').keypress(function(event) {

            var key = (event.keyCode ? event.keyCode : event.which);
            if (key == 13) {

                var comment = $('#' + '<?php echo $status->id;?>').val();
                var status = '<?php echo $status->id;?>';

                $.ajax({
                        method: "POST",
                        url: "file.php",
                        data: {status: status, comment: comment},
                        success: function(status) {
                        }
                    });
            };
        }); 
    });
</script>

And it is not working... But when i set id to some name like comment and put it everywhere like this:

<textarea id="comment"></textarea>

 $('#comment').keypress(function(event) {

var comment = $('#comment').val();

It works but only for first row in database like if I have 3 posts and post ids are 12, 17, 33. And I am commenting on post with id 33 it saves values to post with id 12 only to that one... Any help?

UPDATE:

I am using foreach and for each post from database I am echoing this:

echo "<div id='spacer'></div><div id='statusess' style='background: rgba(255,255,255,0);'><div id='pader' style='background-color:".$statuscolor.";'><div id='statuses' style='background: rgba(255,255,255,0.5);'><div id='rod'><a href='d/".$page."?fromid=".$frid."&toid=".$status->fromid."&forid=".$status->id."'>$rord</a></div> <a href='profile.php?u=".$status->fromid."'> <img src='../juzer/".$status->fromid."/profile/".$status->profilepic."'width='30px' height='30px'> ".$status->fromname."</a><br />".$status->text."<br /><br /><a href='d/npostlikes.php?u=".$frid."&type=status&id=".$status->id."'><img src='d/likes/like.png' width='20px' height='20px'></a>".$count."<textarea id='comm1_".$status->id."'  name='comm1'   onkeyup='textAreaAdjust(this)' style=' resize: none; width: 300px; height: auto; '></textarea>";

The end of echo is my problem and the problem is that when I press key it saves to database only first post id from posts database. So basically i am trying to say that every comments it is storing as comments for only first post from posts database... So I want to add id to each textarea as unique so when ssomeone press key it saves to that id not first one only....

  • 写回答

3条回答 默认 最新

  • weixin_33725515 2016-04-12 14:27
    关注

    You are doing it wrong when trying to build the selector with PHP in the Javascript.

    This:

    $('#'+'<?php echo $status->id;?>')...
    

    Should be:

    $('#<?php echo $status->id;?>')...
    

    You should also add some string to the ID to avoid conflict with other elements having similar IDs. For example:

    $('#comment_<?php echo $status->id;?>')...
    

    EDIT: I misread the last part of your questions, and apparently I only answered partially to it. For the rest would be better if you could post more code so I can help better.

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本