jQuery检查下一个td单选按钮组的单选按钮

时间:2012-11-19 08:54:59

标签: jquery html radio-button

我有一个带单选按钮的表,在第一列,选项是:

  • '---'
  • '没有股票'
  • '有库存'

在第二列,选项为:

  • '---'
  • '没有股票'
  • '低'
  • '中'
  • '高'

现在我想要做的是,当用户点击第一列上的“无库存”时,应检查第二列上的“无库存”单选项。

这就是我正在做的事情:

HTML:

<table border='1'>

    <tr>
        <td class="valign_top" style="text-align:left;">
            <div class="radio_field">
                <input type="radio" class="stock_avail" id="93_stock_avail_0" name="93_stock_avail" value="0" checked=""> ---
            </div>

            <div class="radio_field">
                <input type="radio" class="stock_avail" id="93_stock_avail_No" name="93_stock_avail" value="No"> No Stock
            </div>

            <div class="radio_field">
                <input type="radio" class="stock_avail" id="93_stock_avail_Yes" name="93_stock_avail" value="Yes"> With Stock
            </div>
        </td>

        <td class="sa_sw_td valign_top" style="text-align:left;">                    
            <div class="radio_field">
                <input type="radio" class="sa_sw" name="93_sa_sw" id="93_sa_sw_0" value="0" checked=""> ---
            </div>

            <div class="radio_field">
                <input type="radio" class="sa_sw" name="93_sa_sw" id="93_sa_sw_1" value="1"> No Stock
            </div>

            <div class="radio_field">
                <input type="radio" class="sa_sw" name="93_sa_sw" id="93_sa_sw_2" value="2"> Low
            </div>

            <div class="radio_field">
                <input type="radio" class="sa_sw" name="93_sa_sw" id="93_sa_sw_3" value="3"> Medium
            </div>

            <div class="radio_field">
                <input type="radio" class="sa_sw" name="93_sa_sw" id="93_sa_sw_4" value="4"> High
            </div>
        </td>
    </tr>


    <tr>
        <td class="valign_top" style="text-align:left;">
            <div class="radio_field">
                <input type="radio" class="stock_avail" id="94_stock_avail_0" name="94_stock_avail" value="0" checked=""> ---
            </div>

            <div class="radio_field">
                <input type="radio" class="stock_avail" id="94_stock_avail_No" name="94_stock_avail" value="No"> No Stock
            </div>

            <div class="radio_field">
                <input type="radio" class="stock_avail" id="94_stock_avail_Yes" name="94_stock_avail" value="Yes"> With Stock
            </div>
        </td>

        <td class="sa_sw_td valign_top" style="text-align:left;">                    
            <div class="radio_field">
                <input type="radio" class="sa_sw" name="94_sa_sw" id="94_sa_sw_0" value="0" checked=""> ---
            </div>

            <div class="radio_field">
                <input type="radio" class="sa_sw" name="94_sa_sw" id="94_sa_sw_1" value="1"> No Stock
            </div>

            <div class="radio_field">
                <input type="radio" class="sa_sw" name="94_sa_sw" id="94_sa_sw_2" value="2"> Low
            </div>

            <div class="radio_field">
                <input type="radio" class="sa_sw" name="94_sa_sw" id="94_sa_sw_3" value="3"> Medium
            </div>

            <div class="radio_field">
                <input type="radio" class="sa_sw" name="94_sa_sw" id="94_sa_sw_4" value="4"> High
            </div>
        </td>
    </tr>

</table>​

JavaScript的:

$(document).ready(function(){
    $('.stock_avail').click(function(){

        var value = $(this).val();
        console.log("value: " + value);

        if(value=='No'){
            $(this).next('td').find('radio').val(2);
        }

    });
});​

这可能会有所帮助:http://jsfiddle.net/E6mhM/

任何想法如何做到这一点? TIA !!!

2 个答案:

答案 0 :(得分:1)

$(document).ready(function(){
    $('.stock_avail').click(function(){

        var value = $(this).val();
        console.log("value: " + value);

        if(value=='No'){
            $(this).closest("td").next().find("input[value=1]").attr("checked",true);
        }

    });
});​

演示:http://jsfiddle.net/E6mhM/4/

答案 1 :(得分:0)

$(document).ready(function(){
    $('.stock_avail').click(function(){
        if($(this).val()=='No'){
            $(this).parent().parent().next().find(':radio:eq(1)').attr('checked','checked');
           //assuming No stock will be always a second radio option 
        }
    });
});