英文:
Based on dropdown select value populate input text fields and initially it should have 2 input fields
问题
以下是您提供的内容的翻译:
问题是初始时应显示2个输入文本字段,根据下拉选择添加或删除输入字段,下拉菜单从2开始,到6结束。
我的代码:
function addElements(selectElement) {
var textboxNumbers = selectElement.value;
var dsets = document.getElementById("dsets");
var dmenu = document.getElementById("textboxes");
dsets.innerHTML = ""; //清空"dsets"元素
for (var i = 0; i < textboxNumbers; i++) {
var yourTextboxes = document.createElement("INPUT");
yourTextboxes.setAttribute("type", "textbox" + "<br>");
yourTextboxes.setAttribute("id", "dsid" + (i + 1));
yourTextboxes.setAttribute("placeholder", "DataSet Name " + (i + 1));
yourTextboxes.setAttribute("style", "height: 25px; width: 250px;");
dsets.appendChild(yourTextboxes);
}
document.getElementById("textboxes").style.display = '';
}
<td><select class="displaymenu" onclick="addElements(this)" id="textboxes">
<option>Please Select</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select></td>
</tr>
</table>
预期结果:
任何帮助将不胜感激...
英文:
Problem is initially it should show 2 input text fields and based on drop down select it should add or remove input fields.
and dropdown menu start with 2 till 6.
My Code:
function addElements(selectElement) {
var textboxNumbers = selectElement.value;
var dsets = document.getElementById("dsets");
var dmenu = document.getElementById("textboxes");
dsets.innerHTML = ""; //Empty "dsets" element
for (var i = 0; i < textboxNumbers; i++) {
var yourTextboxes = document.createElement("INPUT");
yourTextboxes.setAttribute("type", "textbox"+"<br>");
yourTextboxes.setAttribute("id", "dsid"+(i+1));
yourTextboxes.setAttribute("placeholder", "DataSet Name "+(i+1));
yourTextboxes.setAttribute("style", "height: 25px; width: 250px;");
dsets.appendChild(yourTextboxes);
}
document.getElementById("textboxes").style.display = '';
}
<td><select class="displaymenu" onclick="addElements(this)" id="textboxes">
<option>Please Select</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select></td>
</tr>
</table>
Expected:
Any Help will be much appreciated...
答案1
得分: 1
以下是翻译好的内容:
您可能没有添加 <div id="dsets"></div>
以容纳要附加的输出。
还要为选择框使用 onchange
事件。我已经更改了它。
在选择框中添加 selected
,它将选择默认选项,并在默认状态下触发选定的选项更改事件。
请尝试以下代码
<!-- 开始片段: js 隐藏: false 控制台: true babel: false -->
<!-- 语言: lang-js -->
function addElements(selectElement) {
var textboxNumbers = selectElement.value;
var dsets = document.getElementById("dsets");
var dmenu = document.getElementById("textboxes");
dsets.innerHTML = ""; // 清空 "dsets" 元素
for (var i = 0; i < textboxNumbers; i++) {
var yourTextboxes = document.createElement("INPUT");
var linebreak = document.createElement("br");
yourTextboxes.setAttribute("type", "textbox" + "<br>");
yourTextboxes.setAttribute("id", "dsid" + (i + 1));
yourTextboxes.setAttribute("placeholder", "DataSet Name " + (i + 1));
yourTextboxes.setAttribute("style", "height: 25px; width: 250px;");
dsets.appendChild(yourTextboxes);
dsets.appendChild(linebreak);
}
document.getElementById("textboxes").style.display = '';
}
var event = new Event('change');
var element = document.querySelector(".displaymenu");
element.dispatchEvent(event);
<!-- 语言: lang-html -->
<table>
<td><select class="displaymenu" onchange="addElements(this)" id="textboxes">
<option>Please Select</option>
<option selected>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select></td>
</table>
<div id="dsets"></div>
<!-- 结束片段 -->
希望这会有所帮助。
<details>
<summary>英文:</summary>
You may not added `<div id="dsets"></div>` to hold the output to append.
Also use `onchange` event for **select boxes**. I have changed it.
Add `selected` to the select box, it will select the default option and trigged the selected options change event in default state.
### Try below Code
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function addElements(selectElement) {
var textboxNumbers = selectElement.value;
var dsets = document.getElementById("dsets");
var dmenu = document.getElementById("textboxes");
dsets.innerHTML = ""; //Empty "dsets" element
for (var i = 0; i < textboxNumbers; i++) {
var yourTextboxes = document.createElement("INPUT");
var linebreak = document.createElement("br");
yourTextboxes.setAttribute("type", "textbox" + "<br>");
yourTextboxes.setAttribute("id", "dsid" + (i + 1));
yourTextboxes.setAttribute("placeholder", "DataSet Name " + (i + 1));
yourTextboxes.setAttribute("style", "height: 25px; width: 250px;");
dsets.appendChild(yourTextboxes);
dsets.appendChild(linebreak);
}
document.getElementById("textboxes").style.display = '';
}
var event = new Event('change');
var element = document.querySelector(".displaymenu");
element.dispatchEvent(event);
<!-- language: lang-html -->
<table>
<td><select class="displaymenu" onchange="addElements(this)" id="textboxes">
<option>Please Select</option>
<option selected>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select></td>
</tr>
</table>
<div id="dsets"></div>
<!-- end snippet -->
Hope this will helps.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论