2012-11-20 4 views
0

나는 드롭 다운이있는 체크 박스 행이 있습니다. 선택한 체크 상자에 해당하는 드롭 다운의 값을 반환하려면 어떻게해야합니까? Jquery를 사용하여 드롭 다운 행에서 고유 한 값을 반환합니다.

http://jsfiddle.net/Gqzhq/21/

$('input[name=options]').live('click', function() { 

    if($(this).hasClass('checked')) 
    { 
    $(this).removeClass('checked'); 

    }else{ 
    $(this).addClass('checked'); 

    } 

    }); 

당신은 siblings()를 사용하여 선택을 대상으로하거나 부모 TD까지 가서 find()을 사용할 수 있습니다

<table id="test" summary="test"> 
<th>A</th> <th>B</th><th>C</th> 

<tr> 
<td><input type="checkbox" name="options" id="1" value="500"> 
     <select class="my_select"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option></td> 
    <td><input type="checkbox" name="options" id="2" value="500"> 
    <select class="my_select"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option></td> 

    <td><input type="checkbox" name="options" id="3" value="500"> 
    <select class="my_select"> 
    <option value="1">1</option> 
    <option value="2">2</option> 
    <option value="3">3</option></td> 

    </tr> 

    </table> 
+0

을 변경'ID'의 숫자로 시작해서는 안된다. 'TH'의 값을''으로 감싸고 표의 몸체가 '' –

답변

2

HTML. 또한 live()은 사용되지 않으며 대신 on()을 사용하십시오. 당신이 toggleClass()를 사용하는 경우

당신은 당신의 if($(this).hasClass('checked'))

$(document).on('click', 'input[name=options]', function() {){ 
    /* "this" within the handler refers to current input*/ 

    var selectVal= $(this).siblings('select').val(); 
      /* OR */ 
    var selectVal= $(this).parent().find('select').val(); 

    /* second argument determines whether to add or remove*/ 
    $(this).toggleClass('checked', this.checked); 

}); 

API 참조를 제거 할 수 있습니다

http://api.jquery.com/siblings/

http://api.jquery.com/toggleClass/

http://api.jquery.com/on/

0

내가 당신은``태그 .. 및 폐쇄 누락 귀하의 예를 http://jsfiddle.net/Gqzhq/27/

+0

이면 jQuery 버전 1.6 이상에서'checked'에'prop()'메소드를 사용해야합니다. – charlietfl

관련 문제