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 badges
Add a ment  | 

3 Answers 3

Reset to default 6

This 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