gpt4 book ai didi

javascript - 如何添加一组子onclick事件

转载 作者:行者123 更新时间:2023-11-30 09:31:54 27 4
gpt4 key购买 nike

我为预订航类编写了以下代码,效果很好。我的问题是我无法在 .button-click a 的点击事件上添加一个组。我想在单击加号时添加带有新类的 div。但它不起作用并重复我最终需要它的另一个选择框。我怎样才能用类重复 div:新的 onclick 事件?

这是我的代码片段:

$(function() {

// Function to create child dropdown
var createChildDropdown = function(i) {

// Create a div in the following format:
// <div class="childs">
// <label for="...">Child (number)</label>
// <select id="..."></select>
// </div>
var $childDropdown = $('<div />', {
'class': 'childs'
});
$childDropdown.append($('<label />', {
'for': 'childDropdown-' + i
}).text('Child ' + i));
$childDropdown.append($('<select />', {
'id': 'childDropdown-' + i
}));

// Define options made available for each child
var options = ['a', 'b', 'c'];
options.forEach(function(option, index) {
//-> added parameter name index here ^^^^^
// Create new option element and append to <select>
$childDropdown.find('select').append($('<option />')
.text(option).attr('value', index));
//-> and using it here ... ^^^^^
});

// Return documentFragment so that we can append it
return $childDropdown;
};

// Function to destroy child dropdown
var destroyChildDropdown = function($el, i) {
$el.find('div.childs').get(i).remove();
};

$(".button-click a").on("click", function() {
var button = $(this);
var oldVal = parseInt(button.closest("ul").prev().val());
var newVal = (button.text() == "+") ? oldVal + 1 : (oldVal > 0) ? oldVal - 1 : 0;
var total_value = "";

button.closest("ul").prev().val(newVal);

$(".travel").each(function() {
var cat = $(this).prev('span').text();
total_value += cat + ": " + $(this).val() + ", ";
});

if (oldVal < newVal) {
$('.childDropdowns').append(createChildDropdown(newVal));
} else if (oldVal > newVal) {
destroyChildDropdown($('.childDropdowns'), newVal);
}

total_value = total_value.substring(0, total_value.length - 2);
$(".main").val(total_value);
})
})
.new{border:1px solid red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<label>
Count Room
<input type="text" name="no_travellers" class="main" value="Room:1, Adult:1" placeholder="" />
</label>
<br/>
<label>
<span>Room</span>
<input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672" value="1" />
<ul class="button-group button-click">
<li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
<li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
</ul>
</label>

<div class="new">
<label>
<span>Adult</span>
<input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672" value="0" />
<ul class="button-group button-click">
<li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
<li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
</ul>
</label>


<label>
<span>Child</span>
<input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672" value="0" />
<ul class="button-group button-click">
<li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
<li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
</ul>
</label>
</div>



<div class="childDropdowns"></div>

最佳答案

在你的 html 中这样做,为新的 div 添加一个包装器 div

<label>
Count Room
<input type="text" name="no_travellers" class="main" value="Room:1, Adult:1" placeholder="" />
</label>
<br/>
<label>
<span>Room</span>
<input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672" value="1" />
<ul class="button-group button-click">
<li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
<li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
</ul>
</label>
<div id="collectnew">
<div class="new">
<label>
<span>Adult</span>
<input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672" value="0" />
<ul class="button-group button-click">
<li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
<li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
</ul>
</label>


<label>
<span>Child</span>
<input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672" value="0" />
<ul class="button-group button-click">
<li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
<li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
</ul>
</label>
</div>
</div>



<div class="childDropdowns"></div>

和你的javascript。代码不受管理,但我所做的是在变量中创建了一个必需的 html 并通过 jquery 附加到

$(function() {

// Function to create child dropdown
var createChildDropdown = function(i) {





// Create a div in the following format:
// <div class="childs">
// <label for="...">Child (number)</label>
// <select id="..."></select>
// </div>
var $childDropdown = $('<div />', {
'class': 'childs'
});
$childDropdown.append($('<label />', {
'for': 'childDropdown-' + i
}).text('Child ' + i));
$childDropdown.append($('<select />', {
'id': 'childDropdown-' + i
}));

// Define options made available for each child
var options = ['a', 'b', 'c'];
options.forEach(function(option, index) {
//-> added parameter name index here ^^^^^
// Create new option element and append to <select>
$childDropdown.find('select').append($('<option />')
.text(option).attr('value', index));
//-> and using it here ... ^^^^^
});

// Return documentFragment so that we can append it
return $childDropdown;
};

// Function to destroy child dropdown
var destroyChildDropdown = function($el, i) {
$el.find('div.childs').get(i).remove();
};

$(".button-click a").on("click", function() {

var html = '<div class="new">'+
'<label>'+
'<span>Adult</span>'+
'<input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672" value="0" />'+
'<ul class="button-group button-click">'+
'<li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>'+
'<li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>'+
'</ul>'+
'</label>'+
'<label>'+
'<span>Child</span>'+
'<input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672" value="0" />'+
'<ul class="button-group button-click">'+
'<li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>'+
'<li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>'+
'</ul>'+
'</label>'+
'</div>';
$("#collectnew").append(html);


var button = $(this);
var oldVal = parseInt(button.closest("ul").prev().val());
var newVal = (button.text() == "+") ? oldVal + 1 : (oldVal > 0) ? oldVal - 1 : 0;
var total_value = "";

button.closest("ul").prev().val(newVal);

$(".travel").each(function() {
var cat = $(this).prev('span').text();
total_value += cat + ": " + $(this).val() + ", ";
});

if (oldVal < newVal) {
$('.childDropdowns').append(createChildDropdown(newVal));
} else if (oldVal > newVal) {
destroyChildDropdown($('.childDropdowns'), newVal);
}

total_value = total_value.substring(0, total_value.length - 2);
$(".main").val(total_value);
});

});

希望这在我的案例中有效,并且不要忘记包含 jquery 文件。我发现它会在单击加号和减号按钮时添加希望你可以检查是否有

关于javascript - 如何添加一组子onclick事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45770770/

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