生成基于下拉菜单中的数值的文本框如何?

huangapple go评论59阅读模式
英文:

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 -->

&lt;script&gt;
  $(&quot;#elementreg&quot;).on(&quot;change&quot;, function() {
  var val = $(this).val();

  $(&quot;.types&quot;).hide().find(&#39;input:text&#39;).val(&#39;&#39;); // 
  if (val) $(&quot;#&quot; + val).show();
  });
&lt;/script&gt;

&lt;select class=&quot;medium&quot; id=&quot;elementreg&quot; name=&quot;IDs&quot;&gt;
    &lt;option value=&quot;&quot; selected=&quot;selected&quot;&gt;&lt;/option&gt;
    &lt;option value=&quot;1&quot;&gt;1&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;2&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;3&lt;/option&gt;
    &lt;option value=&quot;4&quot;&gt;4&lt;/option&gt;

&lt;/select&gt;

&lt;div class=&quot;types&quot; id=&quot;1&quot;&gt;
  &lt;input name=&quot;First Chip ID&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;types&quot; id=&quot;2&quot;&gt;
    &lt;input name=&quot;First Chip ID&quot;&gt;
    &lt;input name=&quot;Second Chip ID&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;types&quot; id=&quot;3&quot;&gt;
    &lt;input name=&quot;First Chip ID&quot;&gt;
    &lt;input name=&quot;Second Chip ID&quot;&gt;
    &lt;input name=&quot;Third Chip ID&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;types&quot; id=&quot;4&quot;&gt;
    &lt;input name=&quot;First Chip ID&quot;&gt;
    &lt;input name=&quot;Second Chip ID&quot;&gt;
    &lt;input name=&quot;Third Chip ID&quot;&gt;
    &lt;input name=&quot;Fourth Chip ID&quot;&gt;
&lt;/div&gt;

<!-- 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(&quot;container&quot;);
  containerElem.innerHTML = &quot;&quot;;
  if (val == &quot;&quot;) return;

  for (let i = 1; i &lt;= val; i++) {
    var elem = document.createElement(&quot;input&quot;);
    elem.setAttribute(&quot;name&quot;, &quot;chip&quot; + i);
    containerElem.append(elem);
  }
}

<!-- language: lang-html -->

&lt;select class=&quot;medium&quot; onchange=&quot;fn1(this)&quot; id=&quot;elementreg&quot; name=&quot;IDs&quot;&gt;
    &lt;option value=&quot;&quot; selected=&quot;selected&quot;&gt;&lt;/option&gt;
    &lt;option value=&quot;1&quot;&gt;1&lt;/option&gt;
    &lt;option value=&quot;2&quot;&gt;2&lt;/option&gt;
    &lt;option value=&quot;3&quot;&gt;3&lt;/option&gt;
    &lt;option value=&quot;4&quot;&gt;4&lt;/option&gt;
&lt;/select&gt;
&lt;div id=container&gt;&lt;/div&gt;

<!-- 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 $(&quot;$elementreg&quot;) 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 -->

&lt;select class=&quot;medium&quot; id=&quot;elementreg&quot; name=&quot;IDs&quot;&gt;
  &lt;option value=&quot;&quot; selected=&quot;selected&quot;&gt;&lt;/option&gt;
  &lt;option value=&quot;1&quot;&gt;1&lt;/option&gt;
  &lt;option value=&quot;2&quot;&gt;2&lt;/option&gt;
  &lt;option value=&quot;3&quot;&gt;3&lt;/option&gt;
  &lt;option value=&quot;4&quot;&gt;4&lt;/option&gt;

&lt;/select&gt;

&lt;div class=&quot;types&quot; id=&quot;1&quot;&gt;
  &lt;input name=&quot;First Chip ID&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;types&quot; id=&quot;2&quot;&gt;
  &lt;input name=&quot;First Chip ID&quot;&gt;
  &lt;input name=&quot;Second Chip ID&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;types&quot; id=&quot;3&quot;&gt;
  &lt;input name=&quot;First Chip ID&quot;&gt;
  &lt;input name=&quot;Second Chip ID&quot;&gt;
  &lt;input name=&quot;Third Chip ID&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;types&quot; id=&quot;4&quot;&gt;
  &lt;input name=&quot;First Chip ID&quot;&gt;
  &lt;input name=&quot;Second Chip ID&quot;&gt;
  &lt;input name=&quot;Third Chip ID&quot;&gt;
  &lt;input name=&quot;Fourth Chip ID&quot;&gt;
&lt;/div&gt;


&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;
  $(&quot;#elementreg&quot;).on(&quot;change&quot;, function() {
    var val = $(this).val();
    $(&quot;.types&quot;).hide().find(&#39;input:text&#39;).val(&#39;&#39;); // 
    if (val) $(&quot;#&quot; + val).show();
  });
&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月18日 12:10:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定