这是我的HTML
{% for song in dj_song_list %}
<div>
<p><h3>{{ song.name }}</h3></p>
<p style="display:none;" class="song_pk">{{ song.id }}</p>
<button type="button" class='vote' onclick="update(this);">Vote as Favourite</button>
</div>
{% endfor %}
我想获取p
带有类的元素内的文本值song_pk
function update() {
console.log($('.song_pk').text());
}
这将打印一个随机的长字符串。如何获得与单击的按钮关联的文本的值?
看看最初发布的JSFiddle(http://jsfiddle.net/d43Lr/4/)
合理的解决方案是使更新功能看起来像这样
function update(element) {
console.log($(element).siblings('.song_pk').text());
}
在原始函数中,您发现所有具有“ song_pk”类的元素-在这里,我们正在查看按钮的同级对象。
(此处的演示:http : //jsfiddle.net/Q9H7T/)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句