簡體   English   中英

選中復選框時動態更改選擇選項

[英]Dynamically Change select option when checkboxes are selected

更改單擊復選框時的選擇選項,我有8個復選框的列表,它動態地計數已選中的復選框,例如,如果我單擊1個復選框,它將顯示Number of Credits Selected:1,如果我單擊2個復選框,它將顯示將會顯示“所選的學分數量”:2並很快,

在復選框下面,我具有選擇選項。我想在選中復選框時動態更改選擇選項。例如:如果我單擊兩個復選框,則我的選擇選項應更改為2 selected,依此類推。

有人可以幫助我實現這一目標!

謝謝!

這是一個演示示例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;
            });

        });

只需將以下行添加到您的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM