1

如何获取用链接、属性 href="" 和文本链接标记的值。我为我的英语道歉。谢谢你。

<ul id="list-item">
<li><input type="checkbox" checked="checked" /><a href="http://localhost/">Item 1</a></li>
<li><input type="checkbox" /><a href="http://localhost/">Item 2</a></li>
<li><input type="checkbox" /><a href="http://localhost/">Item 3</a></li>
<li><input type="checkbox" /><a href="http://localhost/">Item 4</a></li>
<input type="button" id="save" value="Save"/>

4

3 回答 3

1

试试这个:

$('#list-item input[type="checkbox"]:checked').next('a').attr('href');

您可以使用change事件来映射锚点的文本和链接,尝试以下操作:

var anchors = [];
$('#list-item input').change(function(){
   var anchors = $(this).closest('ul').find('input:checked').siblings('a').map(function(){
                    return $(this).text() + ": " +this.href
               }).get()    
})

演示

于 2012-08-15T11:39:00.857 回答
1

我认为您正在努力实现这一目标:

编辑:

$('#list-item input[checked=checked]').each(function(){
    alert($(this).next('a').text() + ': ' + $(this).next('a').attr('href') )
}) 

看演示

于 2012-08-15T11:51:20.793 回答
1

您可以使用该find方法获取元素下元素的值。然后使用 jqueryeach方法获取集合中的值:

像这样的东西。

function getAnchorValues(){
    var checklist = $('#list-item').find($('input[type="checked"]:checked');
    $.each(checklist, function(index, val){
        alert index + ' : ' +  val;
    });
}
于 2012-08-15T11:52:50.543 回答