duannai1883 2011-09-02 14:46
浏览 57
已采纳

动态创建跨度ID以供稍后在jquery中使用

I have some PHP code which runs a loop, spitting out a button and a span, each time it loops, like this:

(inside loop)

echo "<span id=\"sendbutton\"> <input type=\"button\" event='$eventID' id=\"sendsomeone\" value=\"Send a family member\"/> </span>";
echo "<span id=\"nameshere\"></span>";

Later in my code, I have jquery that catches one of the button clicks, and does code, then writes back. What I want it to do is to write back to the span immediately following the button that was clicked.

$('#nameshere').append('Hey! this was the button that you clicked!'); 

Clearly, in my example, it does not quite work the way I want. Each loop creates a span with an id of nameshere, and so when I click any of the buttons, it appends to the topmost span. I could easily use $eventID as the span ID, but I don't know how to accurately reference it in my javascript.

How can I have it append to the button that was clicked? $(this) perhaps? I just don't know how to use it correctly.

Edit, more info:

My click code looks like this:

$('input:button').click(function() {...}

Even more info:

I should have asked more clearly. I can make my span ID unique. I just set it to be $eventID. Done. But how do I call that from in jquery?

  • 写回答

3条回答 默认 最新

  • doulechou0700 2011-09-02 14:52
    关注

    You can't have elements with duplicate ids. Change your Id's to classes and try this.

    $("input.sendsomeone").click(
        $(this).next("span").html('Hey! this was the button that you clicked!')
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示