admin管理员组文章数量:1287151
I have a bunch of dummy links in a jQuery Mobile accordian. By dummy links I mean the that href is just "#". The list is created dynamically by another function and a value is stored in the element by using the value attribute. A loop writes the list like this
'<li value="' + result.ID + '"><a href="#">' + result.Name + '</a></li>'
I am having trouble grabbing that value when I click on the link. I am currently using this event handler
$(document).on('click', '#openCallsList li a', function () {
});
When a link is clicked I want to have the value associated with it so I can grab some data from a database ( I know how to do this) and then create a dialog window (which I also know how to do). I am just lost on how to get the value I stored with the link.
I have a bunch of dummy links in a jQuery Mobile accordian. By dummy links I mean the that href is just "#". The list is created dynamically by another function and a value is stored in the element by using the value attribute. A loop writes the list like this
'<li value="' + result.ID + '"><a href="#">' + result.Name + '</a></li>'
I am having trouble grabbing that value when I click on the link. I am currently using this event handler
$(document).on('click', '#openCallsList li a', function () {
});
When a link is clicked I want to have the value associated with it so I can grab some data from a database ( I know how to do this) and then create a dialog window (which I also know how to do). I am just lost on how to get the value I stored with the link.
Share Improve this question asked Jul 10, 2013 at 13:24 deadsixdeadsix 3751 gold badge8 silver badges25 bronze badges3 Answers
Reset to default 6This will work:
$(document).on('click', '#openCallsList li a', function () {
console.log($(this).closest('li').attr('value'));
});
Although you could grab the value
attribute from the <li>
tag, this is invalid HTML. The value
attribute has been deprecated.
This attribute has been deprecated in HTML 4.01. Therefore its use is no longer remended.
source: http://www.htmlquick./reference/tags/li.html#value
What I would suggest is changing it to something like this:
'<li id="' + result.ID + '"><a href="#">' + result.Name + '</a></li>'
And then use
$(document).on('click', '#openCallsList li a', function () {
var value = $(this).parent().attr('id');
// or you could use the closest('li') function too.
});
Here is full code:
HTML Code:
<ul id ='openCallsList'>
<li value='12'><a href="#">12</a></li>
<li value='13'><a href="#">13</a></li>
<li value='14'><a href="#">14</a></li>
<li value='15'><a href="#">15</a></li>
</ul>
Java Script Code:
$(document).on('click', '#openCallsList li a', function () {
alert($(this).parent().attr('value'));
});
本文标签: javascriptGetting a value of a clicked linkStack Overflow
版权声明:本文标题:javascript - Getting a value of a clicked link - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741234944a2362824.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论