选中复选框后动态更改选择选项

时间:2016-06-02 11:28:29

标签: javascript jquery html

更改单击复选框上的选择选项,我有8个复选框的动态列表,它会选中复选框,例如,如果我单击1复选框,它将显示所选的积分数:1,如果我点击2复选框将显示所选的积分数:2,很快,

在复选框下方我有选择选项,我想在选中复选框时动态更改选项 例如:如果我单击两个复选框,我的选择选项应更改为2选择,依此类推。

有人可以帮助我实现它!

谢谢!

以下是演示示例http://jsfiddle.net/Qac6J/552/

HTML

   <div class="container" >
                <table class="table table-striped">
                    <tr>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                        <td align="center">
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" value="1" class="no_of_credits">
                                </label>
                            </div>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="container">
                <div class="row">
                    <div class="text-center">
                        <h1  class="print_no_of_credits">Number Of Credits Selected : 0</h1>

                    </div>
                </div>
            </div>
        </div>

    <form class="form-group">
                <select class="form-control">
                    <option value="1 selected">1 selected</option>
                    <option value="2 selected">2 selected</option>
                    <option value="3 selected">3 selected</option>
                    <option value="4 selected">4 selected</option>
                    <option value="5 selected">5 selected</option>
                    <option value="6 selected">6 selected</option>
                    <option value="7 selected">7 selected</option>
                    <option value="8 selected">8 selected</option>
                    <option value="9 selected">9 selected</option>
                    <option value="10 selected">10 selected</option>
                    <option value="11 selected">11 selected</option>
                    <option value="12 selected">12 selected</option>
                </select> 
        </form>

JQUERY

$(document).ready(function()
        {
            $('input[type="checkbox"]').click(function()
            {
                var total = ($('.no_of_credits:checked').length)+0;
                $(".print_no_of_credits").html( "<h1>Number Of Credits Selected : "+total+"</h1>" ).length;
            });

        });

1 个答案:

答案 0 :(得分:2)

只需将以下行添加到click event

即可
$('.form-control').val(total + " selected");

所以你的更新代码是:

$('input[type="checkbox"]').click(function() {
    var total = ($('.no_of_credits:checked').length) + 0;
    $(".print_no_of_credits").html("<h1>Number Of Credits Selected : " + total + "</h1>").length;
    $('.form-control').val(total + " selected");
});

<强>

UPDATED FIDDLE

<强>更新

您可以将var total计算修改为

var total=$('.no_of_credits:checked').length