gpt4 book ai didi

javascript - 如何使用新传入值验证输入框(存在于 Div 容器中)的值?

转载 作者:行者123 更新时间:2023-11-29 15:58:18 26 4
gpt4 key购买 nike

我有两个具有多个 as 属性的下拉列表,如果我选择第一个下拉列表,那么我会将 input type checkbox 附加到具有选定行的第二个下拉列表。但是如果我选择第一行并再次取消选择,我会在第二个框中获得重复的选项。

这是我的代码片段

$('.search-box').on("change", function(e) {

var inputVal1 = $(this).val();

$('#checkboxes input:checked').each(function() {

$("#txtSelectedVal").val($("#txtSelectedVal").val()+ $(this).attr('value')+ ", ");

});

var inputVal = (inputVal1) == null ? 0 : inputVal1;
if (inputVal.length) {

$.get("sub_category.php", {term: inputVal}).done(function(data) {

var lclObj = JSON.parse(data);

// $('#checkboxes').empty();

for(var i = 0; i < lclObj.length; i++) {
var lclSubCat = lclObj[i].sub_category1;
$('#checkboxes').append("<input type='checkbox' id='chkBox' value="+lclSubCat+">"+lclSubCat+"<br>");

}
$("#checkboxes :checkbox").each(function() {

var subCat =
$(".txtSelectedValuesSubFromCategory1").html();

var array = subCat.split(",");

$.each(array,function(i) {


$("input[type=checkbox[value='"+array[i]+"']").prop('checked',true);


});
});

});
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group col-sm-6">
<label class="control-label">Category:*</label><br>

<select class="form-control search-box" id="selCategory"
name="selCategory" multiple style="width: 300px;">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</div>

<div class="multiselect col-sm-6">
<label>Sub Category:</label>
<div class="selectBox form-group">
<select class="form-control">
<option value="">Select an option</option>
</select>
<div class="overSelect"></div>
</div>
<div id="checkboxes">

</div>
</div>

我不想在第二个下拉列表中显示重复值。我不想使用 empty() 方法,因为我想保留以前选中的复选框。

最佳答案

下面是一个如何完成的小例子。

有一些步骤:

  • 声明一个数组变量,该变量将在单击复选框时更新 eahc(此处为 checkedCheckboxes)。
  • 当下拉列表的值发生变化时执行 AJAX 调用。
  • 当 AJAX 结束时,您就得到了类别。删除所有已经存在的复选框,然后通过循环为每个类别创建一个新的复选框。 不要忘记检查 checkedCheckboxes 是否包含当前循环类别,以便在需要时重新检查它。
  • 向这些将更新 checkedCheckboxes 的复选框添加事件监听器。

// Here's the variable that keep the checked checkboxes.
let checkedCheckboxes = [];

$('#multiple').on('change', function() {
const values = $(this).val();

// Simulates your AJAX call.
let categories = [];

values.forEach((value) => {
// Avoids to make another loop and to push at each iteration.
categories = [].concat(categories, getCategories(value));
});
// ^^^ Simulating your AJAX call finished.

// Empties the checkbox container.
$('#checkboxes').empty();

// For each got categories...
categories.forEach((cat) => {
// Creates a checkbox <input>.
const checkbox = $('<input>')
.attr('type', 'checkbox')
.val(cat);

// If that category was already checked,
// then check it again.
if (checkedCheckboxes.includes(cat)) {
checkbox.prop('checked', true);
}

// On click on a checkbox...
checkbox.on('change', handleOnClickCheckbox);

// Appends the checkbox to DOM.
$('#checkboxes').append(checkbox, cat);
});
});

// Adds or removes from the concerned variable which checkboxes
// is or isn't checked.
function handleOnClickCheckbox() {
const value = $(this).val(),
isChecked = $(this).is(':checked');

// Keep in memory that that checkbox was checked.
if (isChecked && !checkedCheckboxes.includes(value)) {
checkedCheckboxes.push(value);
} // Removes that checkbox if we are unchecking it.
else if (!isChecked && checkedCheckboxes.includes(value)) {
checkedCheckboxes.splice(checkedCheckboxes.indexOf(value), 1);
}
}

// Simulates ajax.
function getCategories(id) {
const cats = {
'1': ['1-A', '1-B'],
'2': ['2-A'],
'3': ['3-A', '3-B', '3-C']
};

return cats[id];
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="multiple" multiple>
<option value="1">Cat 1</option>
<option value="2">Cat 2</option>
<option value="3">Cat 3</option>
</select>

<div id="checkboxes"></div>


这里是您的代码片段,遵循我之前陈述的步骤:

// Here's the variable that keep the checked checkboxes.
let checkedCheckboxes = [];

$('.search-box').on('change', function (e) {
// "VAL = A || B" syntax is the same as:
// "VAL = (!VAL) ? 0 : VAL"
const inputVal = $(this).val() || 0;

if (inputVal.length > 0) {
$
.get("sub_category.php", {term: inputVal})
.done(function (data) {
const lclObj = JSON.parse(data);

$('#checkboxes').empty();

for (let item of lclObj) {
const cat = item.sub_category1,
jLabel = $(`<label><input type='checkbox' value=${cat}>${cat}</label>`),
jCheckbox = jLabel.find('input');

// If that category was already checked,
// then check it again.
if (checkedCheckboxes.includes(cat)) {
jCheckbox.prop('checked', true);
}

// On click on a checkbox...
jCheckbox.on('change', handleOnClickCheckbox);

// Appends the label (containing the checkbox) to DOM.
$('#checkboxes').append(jLabel);
}
});
}
});

// Adds or removes from the concerned variable which checkboxes
// is or isn't checked.
function handleOnClickCheckbox() {
const value = $(this).val(),
isChecked = $(this).is(':checked');

// Keep in memory that that checkbox was checked.
if (isChecked && !checkedCheckboxes.includes(value)) {
checkedCheckboxes.push(value);
} // Removes that checkbox if we are unchecking it.
else if (!isChecked && checkedCheckboxes.includes(value)) {
checkedCheckboxes.splice(checkedCheckboxes.indexOf(value), 1);
}
}

关于javascript - 如何使用新传入值验证输入框(存在于 Div 容器中)的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56162207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com