gpt4 book ai didi

javascript - 如何将计数器的值替换为特定值

转载 作者:行者123 更新时间:2023-12-03 05:05:51 27 4
gpt4 key购买 nike

我通过更改 num rooms 输入的值来创建一些输入。我想将 counter 的值替换为 span 中的特定值。例如下面的代码:

   for (var i =1; i <= $num; i++){
"<span class=' btn-block room-name'>Room" + i +"</span>"
}

如果 i=1 , 1 替换为跨度中的第一个,结果:房间第一个。i=2 房间第二等。

   <div class="form-group col-sm-4">
<input type="number" class="form-control numRoom-btn" placeholder="num rooms" min="1" max="5" />
</div>
<div id="room-container"> </div>


$(document).ready(function () {
$(".numRoom-btn").change(function () {
switch (this.value) {
case "1":
clearBefore();
addRoom("1");
break;
case "2":
clearBefore();
addRoom("2");

break;
case "3":
clearBefore();
addRoom("3");

break;
case "4":
clearBefore();
addRoom("4");

break;
case "5":
clearBefore();
addRoom("5");

break;
}
});
function addRoom($num) {
for (var i =1; i <= $num; i++) {

var $addedElement = $(
"<div class='numRoom calendar-container'>"+
"<span class=' btn-block room-name'>Room" + i +"</span>" +
"<div class='form-group col-sm-4'>"+
"<span class='btn-block mb5'> Adult(+12)</span>"+
"<input type='number' class='form-control' placeholder='adult nums' min='1' max='9' />"+
"</div>"+
"<div class='form-group col-sm-4'>"+
"<span class='btn-block mb5'>child(-11)</span>"+
"<input type='number' class='form-control' placeholder='child nums' min='0' max='6' />"+
"</div>"+
"</div>");

$('#room-container').append($addedElement);
}
}

function clearBefore() {
$('.numRoom').remove();
}

});

最佳答案

更新的代码:

<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css">
<script type="text/javascript" src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://fontawesome.io/assets/font-awesome/css/font-awesome.css">
<style type="text/css">
</style>
<title>Fontawesome example</title>

<script type="text/javascript">//<![CDATA[
window.onload=function(){

var number= ['zero', 'first', 'second', 'third', 'fourth', 'fifth'];

$(document).ready(function () {
$(".numRoom-btn").change(function () {

switch ($(this).val()) {
case "1":
clearBefore();
addRoom("1");
break;
case "2":
clearBefore();
addRoom("2");
break;
case "3":
clearBefore();
addRoom("3");

break;
case "4":
clearBefore();
addRoom("4");

break;
case "5":
clearBefore();
addRoom("5");

break;
}
});
function addRoom($num) {
for (var i =1; i <= $num; i++) {

$('#room-container').append("<div class='numRoom calendar-container'>"+
"<span class=' btn-block room-name'>Room " + number[i] +"</span>" +
"<div class='form-group col-sm-4'>"+
"<span class='btn-block mb5'> Adult(+12)</span>"+
"<input type='number' class='form-control' placeholder='adult nums' min='1' max='9' />"+
"</div>"+
"<div class='form-group col-sm-4'>"+
"<span class='btn-block mb5'>child(-11)</span>"+
"<input type='number' class='form-control' placeholder='child nums' min='0' max='6' />"+
"</div>"+
"</div>");
}
}

function clearBefore() {
$('.numRoom').remove();
}

});
}//]]>

</script>
</head>
<body>
<div class="form-group col-sm-4">
<input type="number" class="form-control numRoom-btn" placeholder="num rooms" min="1" max="5" />
</div>
<div id="room-container"></div>


</body></html>

关于javascript - 如何将计数器的值替换为特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42005159/

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