英文:
How to Generate textboxes based on a numeric value from Dropdown Menu?
问题
抱歉,您的请求中包含了一些要求我不提供的内容,如代码部分不要翻译和不要回答翻译的问题。以下是翻译好的部分:
Forenote: I am extremely new and inexperienced with HTML and Javascript. Apologies for any inefficiencies or syntax that seems nonsensical.
I am currently trying to include a feature on an HTML page with a drop-down menu containing numeric values from 1 to 4. A corresponding amount of text boxes is generated based on the value chosen in the dropdown menu.
Here is the code I have so far:
<script>
$("#elementreg").on("change", function() {
var val = $(this).val();
$(".types").hide().find('input:text').val(''); //
if (val) $("#" + val).show();
});
</script>
<select class="medium" id="elementreg" name="IDs">
<option value="" selected="selected"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div class="types" id="1">
<input name="First Chip ID">
</div>
<div class="types" id="2">
<input name="First Chip ID">
<input name="Second Chip ID">
</div>
<div class="types" id="3">
<input name="First Chip ID">
<input name="Second Chip ID">
<input name="Third Chip ID">
</div>
<div class="types" id="4">
<input name="First Chip ID">
<input name="Second Chip ID">
<input name="Third Chip ID">
<input name="Fourth Chip ID">
</div>
The result is that nothing occurs when selecting the values on the dropdown menu and the input boxes are not hidden. My suspicion is that I need to include this CSS code snippet:
.types {
display: none;
}
But am out of my depth as to how to approach this. In addition, I am unsure if the script was implemented correctly to begin with. Any and all advice is greatly appreciated.
英文:
Forenote: I am extremely new and inexperienced with HTML and Javascript. Apologies for any inefficiencies or syntax that seems nonsensical.
I am currently trying to include a feature on an HTML page with a drop-down menu containing numeric values from 1 to 4. A corresponding amount of text boxes is generated based on the value chosen in the dropdown menu.
Here is the code I have so far:
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-html -->
<script>
$("#elementreg").on("change", function() {
var val = $(this).val();
$(".types").hide().find('input:text').val(''); //
if (val) $("#" + val).show();
});
</script>
<select class="medium" id="elementreg" name="IDs">
<option value="" selected="selected"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div class="types" id="1">
<input name="First Chip ID">
</div>
<div class="types" id="2">
<input name="First Chip ID">
<input name="Second Chip ID">
</div>
<div class="types" id="3">
<input name="First Chip ID">
<input name="Second Chip ID">
<input name="Third Chip ID">
</div>
<div class="types" id="4">
<input name="First Chip ID">
<input name="Second Chip ID">
<input name="Third Chip ID">
<input name="Fourth Chip ID">
</div>
<!-- end snippet -->
The result is that nothing occurs when selecting the values on the dropdown menu and the input boxes are not hidden. My suspicion is that I need to include this CSS code snippet:
<!-- language: lang-css -->
.types {
display: none
}
<!-- end snippet -->
But am out of my depth as to how to approach this. In addition, I am unsure if the script was implemented correctly to begin with. Any and all advice is greatly appreciated.
答案1
得分: 2
以下是您提供的代码的中文翻译:
也许这个答案对于无需使用jQuery进行动态渲染有所帮助
function fn1(val) {
val = val.value;
var containerElem = document.getElementById("container");
containerElem.innerHTML = "";
if (val == "") return;
for (let i = 1; i <= val; i++) {
var elem = document.createElement("input");
elem.setAttribute("name", "chip" + i);
containerElem.append(elem);
}
}
<select class="medium" onchange="fn1(this)" id="elementreg" name="IDs">
<option value="" selected="selected"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id="container"></div>
英文:
maybe this answer helpful for dynamic rendering without jquery
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function fn1(val) {
val = val.value;
var containerElem = document.getElementById("container");
containerElem.innerHTML = "";
if (val == "") return;
for (let i = 1; i <= val; i++) {
var elem = document.createElement("input");
elem.setAttribute("name", "chip" + i);
containerElem.append(elem);
}
}
<!-- language: lang-html -->
<select class="medium" onchange="fn1(this)" id="elementreg" name="IDs">
<option value="" selected="selected"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div id=container></div>
<!-- end snippet -->
答案2
得分: 1
问题在于脚本在DOM元素呈现之前就被执行了。所以$("$elementreg")
无法找到要附加onChange
监听器的元素。
最好将脚本放在body的末尾,或者使用$( document ).ready()
$("#elementreg").on("change", function() {
var val = $(this).val();
$(".types").hide().find('input:text').val(''); //
if (val) $("#" + val).show();
});
英文:
The problem is that the script gets executed before your DOM elements are rendered. So $("$elementreg")
is not able to find the element for attaching the onChange
listener.
So it's better to keep the scripts at the end of the body or use $( document ).ready()
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.types {
display: none;
}
<!-- language: lang-html -->
<select class="medium" id="elementreg" name="IDs">
<option value="" selected="selected"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<div class="types" id="1">
<input name="First Chip ID">
</div>
<div class="types" id="2">
<input name="First Chip ID">
<input name="Second Chip ID">
</div>
<div class="types" id="3">
<input name="First Chip ID">
<input name="Second Chip ID">
<input name="Third Chip ID">
</div>
<div class="types" id="4">
<input name="First Chip ID">
<input name="Second Chip ID">
<input name="Third Chip ID">
<input name="Fourth Chip ID">
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$("#elementreg").on("change", function() {
var val = $(this).val();
$(".types").hide().find('input:text').val(''); //
if (val) $("#" + val).show();
});
</script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论