表中的选项选择不会改变

时间:2012-03-23 22:03:25

标签: jquery html

    <html>
    <head>
    <link type="text/css" rel="stylesheet" `href="file:///C:/Users/Documents/style.css" />
    <script type="text/javascript" src="file:///C:/Users/Documents/jquery-`1.7.1.js"></script>
    <script type="text/javascript" `src="file:///C:/Users/Documents/jquery.tablesorter.js"></script>

<script type="text/javascript">
$(function() {

    $('#hpTable input').click(function() 
    {
        var $input = $(this);
        var $inputTD = $input.closest("tr");
        if( $input.prop( 'checked' ) )
        {
            var code = $inputTD.children( '#name' ).attr( "value" );
            alert( "code " + code );

            //var $numberList = $( '#popFields select:option[value=' + code + ']' ).attr( "selected", "selected" );
            //$( '#hpTable select:option[value=' + code + ']' ).attr( "selected", "selected" );
            //$numberList.val( $inputTD.children( '#numberList eq[value=' + code + ']' ).attr( "selected", "selected" ) );
            //$numberList.eq( code ).attr( 'selected', 'selected' );
            //$inputTD.find( '#numberList option' ).eq( code ).attr( 'selected', 'selected' );
            $inputTD.closet( 'select option' ).eq( '3' ).attr( 'selected', true );
         }
    });
});

</script>
</head>
<body>
<table id="popFields">
<thead></thead>
<tbody>
    <tr><td>
        <select id="numberList">
            <option value="1" selected>one</option>
            <option value="2">two</option>
            <option value="3">three</option>
        </select>
    </td></tr>
</tbody>
</table>
<br>
<table id="hpTable" cellpadding="5" width="100%" style="table-layout:fixed" class="tablesorter">
<thead><tr>
        <th width="9%"></th>
        <th width="9%">Date</th>
        <th width="9%">Name</th>
        <th>File</th>
    </tr>
</thead>
<tbody>
    <tr>
        <td><input type="checkbox"/></td>
        <td id="date">3/3/2003</td>
        <td id="name" value="3">Bob</td>
        <td id="file">filename.doc</td>
    </tr>
</tbody>
</table>
</body>
</html>

大家好,再回来......这里有两张桌子,当用户点击一个复选框时,我想在下拉列表中显示该值。我似乎已经尝试了很多东西,但现在变得非常沮丧。我真的很感激任何帮助!

顺便问一下,任何人都可以推荐一本好的jQuery书吗?谢谢!

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

jsBin demo

$(function() {

    $('#hpTable input[type="checkbox"]').change(function() 
    {
        var $input = $(this);
        var $inputTD = $input.closest("tr");
        if( $input.prop( 'checked' ) )
        {
            var code = $inputTD.children( '#name' ).attr( "value" );
            $('#test').html('clicked = '+ code );
            $('select#numberList option[value='+code+']').prop('selected', true);
         }
    });
});