错误:使用JQuery从列表中选择单选按钮

时间:2018-08-30 11:59:48

标签: jquery

我有一个代码,我在其中使用.prop()方法选择单选按钮。问题是某个时候未选择第二个方法(付款方式),脚本崩溃并显示错误,因为这是我用来选择单选按钮的代码。

/**
 * Select shipping method when selected from the list
 */
$(document).ready(function() {
    var $options = $('.shipping-method .item');

    $options.click(function(e) {
        var $current = $(this);
        e.preventDefault()
        e.stopPropagation()
        $options.removeClass('on')
        $current.addClass('on')
        $('input', $current).prop('checked', true)
    })
})
/**
 * Select payment method when selected from the list
 */
$(document).ready(function() {
    var $options = $('.payment-method .item');

    $options.click(function(e) {
        var $current = $(this);
        e.preventDefault()
        e.stopPropagation()
        $options.removeClass('on')
        $current.addClass('on')
        $('input', $current).prop('checked', true)
    })
})

HTML

<ul>
<li>
    <div class="item"><a href="javascript:;" class="shipping_3">Home Delivery</a>
        <input name="shipping" type="radio" class="radio" id="shipping_3" value="3" checked="checked" supportcod="1" insure="0" onclick="selectShipping(this)">
    </div>
</li>
<li>
    <div class="item"><a href="javascript:;" class="shipping_8">Self-pickup</a>
        <input name="shipping" type="radio" class="radio" id="shipping_8" value="8" supportcod="1" insure="0" onclick="selectShipping(this)">
    </div>
</li>

0 个答案:

没有答案