1

我有一个小表格。我想在选择所有单选按钮时启用提交按钮。

下面是我的代码html

<form action="#" id="form1" method="post">
                <ul>
                    <li>
                        <h3>Here’s what I want:<strong class="result1"></strong></h3>
                        <div>
                        <span><input type="radio" value="Cash Back rewards" name="want" id="01"  /><label for="01">Cash Back rewards</label></span>
                        <span><input type="radio" value="Travel rewards" name="want" id="02" /><label for="02">Travel rewards</label></span>
                        <span><input type="radio" value="Gas rewards" name="want" id="03" /><label for="03">Gas rewards</label></span>
                        <span><input type="radio" value="Shopping rewards" name="want" id="04" /><label for="04">Shopping rewards</label></span>
                    </div>
                    </li>

                    <li>
                        <h3>Here’s my credit story: <strong class="result2"></strong></h3>
                        <div>
                        <span><input type="radio" value="I’m new to credit" name="story" id="05" /><label for="05">I’m new to credit</label></span>
                            <span><input type="radio" value="I pay my bills on time" name="story" id="06" /><label for="06">I pay my bills on time</label></span>
                        <span><input type="radio" value="I’ve had credit issues in the past" name="07" id="issues" /><label id="07">I’ve had credit issues in the past</label></span>
                        </div>
                    </li>

                    <li>
                        <h3>Here’s what I carry:</h3>
                        <span><input type="radio" value="I have a credit card with a good record" name="carry" id="08" /><label for="08">I have a credit card with a good record</label></span>
                        <span><input type="radio" value="I don’t have a credit card with a good record" name="carry" id="09" /><label for="09">I don’t have a credit card with a good record</label></span>

                    </li>
                </ul>
                <input type="submit" value="" name="" class="find" />
                </form>

我在javascript方面很弱,请告诉我。

如果 li 不修复,另一件事将动态生成,所以我将不得不做。基本上每个 li 都是一个问题,单选按钮是选项。所以问题将动态生成,它可以是任何否。它不修复

4

1 回答 1

3

像这样的东西可以解决问题:

$('#form1 :radio').click(function() {
    var disabled = $('#form1 li').filter(function() { return $(':radio:checked', this).length == 0; }).length > 0;
    $('#form1 .find').prop('disabled', disabled);
});

这假设您的提交按钮在页面加载时被禁用,因为禁用状态仅在选中选项时才会更改。您当然可以在 DOMReady 期间进行相同的检查(在这种情况下,我将提取侦听器函数),但我更喜欢在加载期间手动设置标记。

演示

通过选择表单中的所有元素,将其减少为仅满足过滤条件“不包含选中的单选按钮”的元素,并检查减少的集合中是否有任何元素disabled来设置变量。li

如果您使用的 jQuery 版本低于 1.6,请使用.attr而不是.prop设置禁用状态。

于 2012-08-07T07:40:41.467 回答