[英]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.