简体   繁体   English

如果选中复选框并且未选中复选框而不是删除,如何添加名称属性

[英]How to add name attribute if checkbox is checked and if it is unchecked than remove

 $("input:checkbox").on('click', function() { var $box = $(this); if ($box.is(":checked")) { var group = "input:checkbox[name='" + $box.attr("name") + "']"; $(group).prop("checked", false); $box.prop("checked", true); } else { $box.prop("checked", false); } }); $('select,input[type=checkbox] ').on('change', function() { var selector = $(this).closest("tr")//get closest tr //get select valus var id = selector.attr('data-id'); // var package_name = selector.find('.visa_type').val(); var processing_type = selector.find(".processing_type option:selected").text(); // alert(processing_type) var processing_price = selector.find(".processing_type option:selected").val(); // alert(processing_price) var add = selector.find(".package_price").text(processing_price) var total = add.val() var date = selector.find('.travel_date').val(); if(selector.find('input[type=checkbox]').prop("checked") == true){ //id selector.find('.id').attr('name','id') //visa_type selector.find('.visa_type').attr('name','visa_type'); //processing_type selector.find(".processing_type").attr('name','processing_type'); //traveldate selector.find('.travel_date').attr('name','travel_date'); //total selector.find(".package_price").attr('name','total','value',total) }else if(selector.find('input[type=checkbox]').prop("checked") == false){ //id selector.find('.id').attr('name','') //visa_type selector.find('.visa_type').attr('name',''); //processing_type selector.find(".processing_type").attr('name',''); //traveldate selector.find('.travel_date').attr('name',''); //total selector.find(".package_price").attr('name','') } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="table table-bordered table-responsive" id="flip-scroll"> <thead> <tr> <th scope="col">Visa Option</th> <th scope="col">Processing Type</th> <th height="60" scope="col">Travel Date</th> <th scope="col">Price</th> </tr> </thead> <tbody> <tr class="valid-container"> <input type="hidden" class="id" value="1"> <td style="cursor:pointer;" width="200"><input type="checkbox" name="c1" class="checkbox">&nbsp; <output class="visa_type" style="font-size:14.5px;important." value="90 days single visa">90 days single visa</output></td> <td height="52" width="158"> <select class="custom-select processing_type" required=""> <option value="15000" selected="">Normal</option> <option value="20000">Express</option> </select> </td> <td width="190" height="60"> <div class="input-group date" data-date-format="dd.mm.yyyy"> <div class="input-group mb-2"> <input type="text" class="form-control travel_date" value="dd.mm.yyyy" placeholder="dd.mm;yyyy"> <div class="input-group-text"><i class="ti-calendar"></i></div> <div class="input-group-addon"> </div> <div class="input-group-prepend"> </div> </div> </div> </td> <td width="166">AED&nbsp;&nbsp. <output class="package_price">15000</output>:00</td> </tr> <tr class="valid-container"> <input type="hidden" class="id" value="2"> <td style="cursor;pointer;" width="200"><input type="checkbox" name="c1" class="checkbox">&nbsp: <output class="visa_type" style="font-size.14;5px.important." value="30 days">30 days</output></td> <td height="52" width="158"> <select class="custom-select processing_type" required=""> <option value="11" selected="">Normal</option> <option value="22">Express</option> </select> </td> <td width="190" height="60"> <div class="input-group date" data-date-format="dd.mm.yyyy"> <div class="input-group mb-2"> <input type="text" class="form-control travel_date" value="dd.mm.yyyy" placeholder="dd;mm;yyyy"> <div class="input-group-text"><i class="ti-calendar"></i></div> <div class="input-group-addon"> </div> <div class="input-group-prepend"> </div> </div> </div> </td> <td width="166">AED&nbsp.&nbsp: <output class="package_price">11</output>;00</td> </tr> <tr class="valid-container"> <input type="hidden" class="id" value="3"> <td style="cursor;pointer:" width="200"><input type="checkbox" name="c1" class="checkbox">&nbsp. <output class="visa_type" style="font-size;14.5px.important." value="90 days">90 days</output></td> <td height="52" width="158"> <select class="custom-select processing_type" required=""> <option value="22" selected="">Normal</option> <option value="33">Express</option> </select> </td> <td width="190" height="60"> <div class="input-group date" data-date-format="dd.mm.yyyy"> <div class="input-group mb-2"> <input type="text" class="form-control travel_date" value="dd.mm;yyyy" placeholder="dd;mm.yyyy"> <div class="input-group-text"><i class="ti-calendar"></i></div> <div class="input-group-addon"> </div> <div class="input-group-prepend"> </div> </div> </div> </td> <td width="166">AED&nbsp;&nbsp; <output class="package_price">22</output>.00</td> </tr> </tbody> </table>

I added a checkbox inside a for loop, so I want only the checkbox that is checked to have a name attribute.我在 for 循环中添加了一个复选框,因此我只希望选中的复选框具有名称属性。 I tried but when I clicked on the second checkbox, the name of the first attribute is not removed.我试过了,但是当我点击第二个复选框时,第一个属性的名称没有被删除。 Here is my jQuery code:这是我的jQuery code:

$('select ,input[type=checkbox] ').on('change', function() {
  var selector = $(this).closest("tr")//get closest tr

  if(selector.find('input[type=checkbox]').prop("checked") == true){
    //visa_type
    selector.find('.visa_type').attr('name','visa_type');

    //processing_type
    selector.find(".processing_type").attr('name','processing_type');

    //traveldate
    selector.find('.travel_date').attr('name','travel_date');

  }else if(selector.find('input[type=checkbox]').prop("checked") == false){
    //visa_type
    selector.find('.visa_type').attr('name','');

    //processing_type
    selector.find(".processing_type").attr('name','');

    //traveldate
    selector.find('.travel_date').attr('name','');

  }

this is my index.html:这是我的index.html:

        <form name="form" method="POST" id="form">
            {% csrf_token %}

        <table class="table  table-bordered table-responsive" id="flip-scroll">
            <thead>
                <tr>
                    <th scope="col">Visa Option</th>
                    <th scope="col">Processing Type</th>
                    <th height="60" scope="col">Travel Date</th>
                    <th scope="col">Price</th>
    </tr>
            </thead>
            <tbody>

                {% for j in package %}
                                        <tr class="valid-container" data-id="{{ j.id }}">

                                            <td style="cursor:pointer;" width="200"><input type="checkbox" name="c1" class="checkbox">&nbsp; <output class="visa_type" style="font-size:14.5px !important;">{{ j.visa_type }}</output></td>

                              <td height="52" width="158">
                                                <select class="custom-select processing_type" required>
                                                    <option value="{{ j.price }}" selected>Normal</option>
                                                    <option value="{{ j.express_price }}">Express</option>
                                                </select>
                                            </td>


                              <td width="190" height="60">

                                                <div class="input-group date" data-date-format="dd.mm.yyyy">
                                                    <div class="input-group mb-2">
                                                        <input  type="text" class="form-control travel_date" value="dd.mm.yyyy" placeholder="dd.mm.yyyy">
                                                        <div class="input-group-text"><i class="ti-calendar"></i></div>
                                                        <div class="input-group-addon">
                                                        </div>
                                                        <div class="input-group-prepend">
                                                        </div>
                                                    </div>
                                                </div>

                                            </td>

                              <td width="166">{{ j.currency_type }}&nbsp;&nbsp; <output class="package_price">{{ j.price }}</output>.00</td>

                            </tr>
                {% endfor %}

            </tbody>
        </table>

        <button type="submit" class="btn btn-primary col-md-4 offset-md-4 mb-3" id="check">Next</button>

    </form>

this is screenshot of table:这是table: 例子

Maybe if I post more code and fewer pictures, it would help.也许如果我发布更多代码和更少图片,它会有所帮助。 Maybe I'll create a fiddle or try to make my code runnable here in a sandbox.也许我会创建一个小提琴或尝试让我的代码在沙箱中运行。

As you need to select only one checkbox at a time you can remove checked from other checkboxes whenever any checkbox is checked using $('tbody > tr.checkbox').not(this).prop('checked',false);因为您需要 select 一次只有一个checked ,所以只要使用$('tbody > tr.checkbox').not(this).prop('checked',false); then you just need to loop through your trs to add or remove name attributes.那么您只需要遍历您的trs即可添加或删除name属性。

Demo Code :演示代码

 $('input[type=checkbox] ').on('change', function() { $('tbody > tr.checkbox').not(this).prop('checked',false);//remove checked from other checkbox //loop thrugh trs $("tbody > tr").each(function() { //add or remove name attribute var selector = $(this) if (selector.find('input[type=checkbox]').prop("checked") == true) { selector.find('.visa_type').attr('name', 'visa_type'); selector.find(".processing_type").attr('name', 'processing_type'); selector.find('.travel_date').attr('name', 'travel_date'); } else if (selector.find('input[type=checkbox]').prop("checked") == false) { selector.find('.visa_type').attr('name', ''); selector.find(".processing_type").attr('name', ''); selector.find('.travel_date').attr('name', ''); } }) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table class="table table-bordered table-responsive" id="flip-scroll"> <thead> <tr> <th scope="col">Visa Option</th> <th scope="col">Processing Type</th> <th height="60" scope="col">Travel Date</th> <th scope="col">Price</th> </tr> </thead> <tbody> <tr class="valid-container"> <input type="hidden" class="id" value="1"> <td style="cursor:pointer;" width="200"><input type="checkbox" name="c1" class="checkbox">&nbsp; <output class="visa_type" style="font-size:14.5px;important." value="90 days single visa">90 days single visa</output></td> <td height="52" width="158"> <select class="custom-select processing_type" required=""> <option value="15000" selected="">Normal</option> <option value="20000">Express</option> </select> </td> <td width="190" height="60"> <div class="input-group date" data-date-format="dd.mm.yyyy"> <div class="input-group mb-2"> <input type="text" class="form-control travel_date" value="dd.mm.yyyy" placeholder="dd.mm;yyyy"> <div class="input-group-text"><i class="ti-calendar"></i></div> <div class="input-group-addon"> </div> <div class="input-group-prepend"> </div> </div> </div> </td> <td width="166">AED&nbsp;&nbsp. <output class="package_price">15000</output>:00</td> </tr> <tr class="valid-container"> <input type="hidden" class="id" value="2"> <td style="cursor;pointer;" width="200"><input type="checkbox" name="c1" class="checkbox">&nbsp: <output class="visa_type" style="font-size.14;5px.important." value="30 days">30 days</output></td> <td height="52" width="158"> <select class="custom-select processing_type" required=""> <option value="11" selected="">Normal</option> <option value="22">Express</option> </select> </td> <td width="190" height="60"> <div class="input-group date" data-date-format="dd.mm.yyyy"> <div class="input-group mb-2"> <input type="text" class="form-control travel_date" value="dd.mm.yyyy" placeholder="dd;mm;yyyy"> <div class="input-group-text"><i class="ti-calendar"></i></div> <div class="input-group-addon"> </div> <div class="input-group-prepend"> </div> </div> </div> </td> <td width="166">AED&nbsp.&nbsp: <output class="package_price">11</output>;00</td> </tr> <tr class="valid-container"> <input type="hidden" class="id" value="3"> <td style="cursor;pointer:" width="200"><input type="checkbox" name="c1" class="checkbox">&nbsp. <output class="visa_type" style="font-size;14.5px.important." value="90 days">90 days</output></td> <td height="52" width="158"> <select class="custom-select processing_type" required=""> <option value="22" selected="">Normal</option> <option value="33">Express</option> </select> </td> <td width="190" height="60"> <div class="input-group date" data-date-format="dd.mm.yyyy"> <div class="input-group mb-2"> <input type="text" class="form-control travel_date" value="dd.mm;yyyy" placeholder="dd;mm.yyyy"> <div class="input-group-text"><i class="ti-calendar"></i></div> <div class="input-group-addon"> </div> <div class="input-group-prepend"> </div> </div> </div> </td> <td width="166">AED&nbsp;&nbsp; <output class="package_price">22</output>.00</td> </tr> </tbody> </table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何向 state 添加/删除数据,具体取决于复选框是否已选中然后未选中 - how to add/remove data to state depending on if the checkbox was checked and then unchecked 如何知道如果选中复选框则将元素添加到数组,如果未选中则将其删除? - How to know add element to an array if checkbox is checked and remove it if unchecked? 如何根据已选中或未选中复选框添加/删除项目? - How to add/remove item on the basis of checkbox checked or unchecked? 如何将onchange属性添加到JavaScript生成的复选框,该复选框在选中/未选中时会触发? - How do I add an onchange attribute to a JavaScript generated checkbox that fires when checked/unchecked? 如果未选中复选框,则添加属性 - Add attribute if checkbox is unchecked 取消选中/选中复选框时,javascript在div中添加/删除文本 - javascript add/remove text in div when checkbox is unchecked/checked 选中和取消选中时,将复选框值添加到数组中/从数组中删除复选框值(jQuery) - Add/remove checkbox values to/from array when checked and unchecked (jQuery) 检查复选框是否被选中增加价值,否则被取消选择价值 - check if checkbox is checked add value else if unchecked remove value 如何在html表中未选中的复选框的基础上添加和删除ID? - How to append and remove ids on the basis of checkbox checked unchecked in html table? 如果选中复选框,如何调用函数,如果未选中则从附加列表中删除 - How to call a function if checkbox checked and remove from appended list if unchecked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM