2015-10-20 37 views
1

我需要从一个选择的值取一个列表GROP一些值(文字和ID):如何从jQuery的列表中获取选定的值?

<div class="list-group col-lg-10" >    
    <a href="#" id="id1" class="list-group-item col-lg-6">Example 1</a> 
    <a href="#" id="id2" class="list-group-item col-lg-6">Example 2</a>  
</div> 

我想带他们只点击一个按钮后。我如何获得这些值?

回答

2

UPDATE使用$(this).attr('id')和文本的ID 2

StoreValue = []; //Declare array 
 
$(".list-group a").click(function(){ 
 
    StoreValue = []; //clear array 
 
    StoreValue.push($(this).attr("id")); //add id to array 
 
    StoreValue.push($(this).text()); // add text to array 
 
}) 
 
$(".button").click(function(){ 
 
    alert(StoreValue[0] + " " + StoreValue[1]) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="list-group col-lg-10" >    
 
    <a href="#" id="id1" class="list-group-item col-lg-6">Example 1</a> 
 
    <a href="#" id="id2" class="list-group-item col-lg-6">Example 2</a>  
 
</div> 
 
<div class="button">Click here</div>

+0

我不想所有名单价值但只有我选择的东西。 – db92

+0

ok看到我的更新 –

+0

点击只需要按钮上的作品。我想过这个方法:在点击标签后存储该值,然后在点击按钮之后使用它。 – db92

0
$('a').click(function() { 
    var id = $(this).attr('id') 
    var text = $(this).html() 


    console.log(id) 
    console.log(text) 
}) 

$('#get').click(function() { 
    var id1 = $('#id1').attr('id') 
    var text1 = $('#id1').html() 

    var id2 = $('#id2').attr('id') 
    var text2 = $('#id2').html() 
    console.log(id1) 
    console.log(text1) 
    console.log(id2) 
    console.log(text2) 
}) 

创建一个事件点击内得到使用$(this).html()

demo

相关问题