简体   繁体   English

如何更改已在 JQuery 中选中的先前复选框的值?

[英]How to change the value of a previous check box that has been selected in JQuery?

Here is what I'm trying to do.这是我正在尝试做的。

I have the ability to create a form when there is an option to create one in Javascript.当 Javascript 中有创建表单的选项时,我可以创建表单。

Here is an option to add an address:这是添加地址的选项:

    <a href="javascript:" onclick="addNewAddress();" id="newAddressUrl"><i class="fas fa-plus-circle"></i>  <span class="newUrlText">add new address</a>

Here is the form in question:这是有问题的表格:

  <input type="text" class="form-control" id="new-address-1-%ID%" name="new-address-1-%ID%" placeholder="Address Line 1">

  <input type="text" class="form-control" id="new-address-2-%ID%" name="new-address-2-%ID%" 
placeholder="Address Line 2">

 <label for="message" class="control-label"><span class="billable-text"><?php _e('Primary Address'); ?></span></label>
                <label class="switch" for="primary-%ID%" style="margin-left: 10px;top: 10px;">
                    <input type="checkbox"  class="primary-%ID%" name="primary-%ID%" id="primary-%ID%" />
                    <div class="slider round gray"></div>
                </label>

Here is the javascript that generates the form:这是生成表单的 javascript:

  <script language="javascript">
    var newAddressIndex = 1;
    var addressarray = [];

    function addNewAddress() {
       var container = getElement("addressContainer");  

        addressarray.push(newAddressIndex);
    
         var htmlAddress = '<?php echo addslashes(App_String::stripBreaksTabsMultipleWhitespace($newAddress)); ?>';
        htmlAddress =  htmlAddress.replace(/%ID%/g, newAddressIndex);

        var node = document.createElement("div");
        node.innerHTML = htmlAddress;

        container.appendChild(node);
        $('#newAddressCount_'+newAssetIndex).val(newAssetIndex);    
        
        ++newAddressIndex;

        test(addressarray);
    }

What I'm trying to do is the following:我想做的是以下内容:

If the user selects the checkbox and then decides to select the next checkbox, I would like to change the previous checkbox from selected to no selected.如果用户选中复选框然后决定 select 下一个复选框,我想将上一个复选框从选中更改为未选中。

How would I go about doing that?我将如何 go 这样做?

Thank you谢谢

I found a solution:我找到了一个解决方案:

What I did was, that I created a function to get the current value of the checkboxes like this:我所做的是,我创建了一个 function 来获取复选框的当前值,如下所示:

    getAddrValue(addressarray);

Then within the function the follwoing:然后在 function 中进行以下操作:

   function getAddrValue (addressarray) {
      $('#primary-'+index).change(function() {
          var checked = this.checked ? 1 : 0;

          prevCheckbox = index-1;

         if (checked == 1) {
                if (document.getElementById('primary-'+prevCheckbox) !== null ) {
                        let inputs = document.getElementById('primary-'+prevCheckbox);
                        inputs.checked = false;
                    }
         }                      
      });
   }
     

暂无
暂无

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

相关问题 jQuery-检查文本框中的特定单词是否已删除并更改元素的类 - jQuery - Check if specific word in text box has been deleted and change class of element 如何检查是否<inputb />在一个值之后有一个值<inputa />已被选中? - How to check if <InputB /> has a value after a value for <InputA /> has been selected? 在触发上一个更改事件并将动态选项加载到该选择框中之后,如何更改选择框的值 - How to change a value of a select box after a previous change event has fired and loaded dynamic options into that select box 你能检查$(this).addClass(&#39;selected&#39;)是否已经添加到jQuery中 - Can you check if $(this).addClass('selected') has already been added in jquery 如何使用JQuery更改onClick复选框的值? - How to change the value of a check box onClick using JQuery? 如何检查函数是否已被调用? jQuery的 - How to check if a function has been called? jQuery 使用jQuery检查并查看选择框是否具有/或包含选择的选项 - Using jQuery to check and see if a select box has/or contains selected options 如果已通过“选择”框的更改事件“选择”或“取消选择”一个选项,则查找 - FInd if an option has been 'selected' OR 'de-selected' via change event of Select box 如何检查在javascript中选择了哪个单选按钮? - How to check which radio button has been selected in javascript? 如何将下拉列表中选定项的值替换为jQuery中文本框的更改事件的某个值? - how to replace value of drop down list selected item to some value on change event of text box in jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM