jQuery:在输出中添加数组索引值

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

jQuery: Add Array Index Value in Output

问题

var input = $('<input placeholder="Enter Subject or Title..." type="text" name="subject1" required/>');
// ...
function addFields(n) {
  for (i = newFields.length; i < n; i++) {
    var newInput = input.clone().attr('name', 'subject' + (i + 1));
    var fieldTitle = 'Subject ' + (i + 1) + ': ';
    var newInputWithLabel = $('<div>').html(fieldTitle).append(newInput);
    newFields = newFields.add(newInput);
    newInputWithLabel.appendTo('#newFields');
  }
}
// ...
英文:

I'm new to jQuery & JavaScript.

Managed to get the following code to work such that it adds a new text input field depending upon the no. selected by the user from a (previous) drop-down select field.

&lt;script&gt;
$(function() {

  var input = $(&#39;&lt;input placeholder=&quot;Enter Name or Title...&quot; type=&quot;text&quot; required/&gt;&#39;);
  var newFields = $(&#39;&#39;);

  $(&#39;#qty&#39;).bind(&#39;blur keyup change&#39;, function() {
    var n = this.value || 0;
    if (n + 1) {
      if (n &gt; newFields.length) {
        addFields(n);
      } else {
        removeFields(n);
      }
    }
  });

  function addFields(n) {
    for (i = newFields.length; i &lt; n; i++) {
      var newInput = input.clone();
      newFields = newFields.add(newInput);
      newInput.appendTo(&#39;#newFields&#39;);
    }
  }

  function removeFields(n) {
    var removeField = newFields.slice(n).remove();
    newFields = newFields.not(removeField);
  }
});
&lt;/script&gt;

However, in the &lt;input placeholder=&quot;Enter Subject or Title...&quot; type=&quot;text&quot; required/&gt;, I want to add two attributes/parameters for each field that gets added:

  1. first is a name=&quot;subject1&quot;, name=&quot;subject2&quot; & so on for each input field

Like, the output input tag should be &lt;input placeholder=&quot;Enter Subject or Title...&quot; type=&quot;text&quot; name=&quot;subject1&quot; required/&gt; for the first field

  1. Second is a field title ("Subject 1", "Subject 2" & so on) just before the &lt;input&gt; tag
    Like, the output input tag should be "Subject 1: &lt;input placeholder=&quot;Enter Subject or Title...&quot; type=&quot;text&quot; name=&quot;subject1&quot; required/&gt;" for the first field

Any inputs please on how to achieve that?

答案1

得分: 1

这是一种添加主题标题和名称属性的方法。

您可以使用字符串连接来使用变量 i 来实现它。

$(function() {
  $('#qty').bind('blur keyup change', function() {
    var n = this.value || 0;
    createFields(n)
  });

  function createFields(n) {
    $("#newFields").empty(); // 清空字段列表

    for (var i = 1; i <= n; i++) {
      var fieldWrapper = $('<div class="fieldwrapper"/>'); // 创建包装器
      var name = $("<p>Subject " + i + "</p>"); // 创建主题标题
      var input = $('<input name="Subject' + i + '" placeholder="Enter Name or Title..." type="text" required />'); // 创建输入框

      fieldWrapper.append(name); // 添加标题
      fieldWrapper.append(input); // 添加输入框
      $("#newFields").append(fieldWrapper); // 添加到列表
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="qty" type="number" />
<div id="newFields">

</div>
英文:

Here is a way to add a subject title and the name property.

You can use string concatenation to achieve it with the variable i.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(function() {
  $(&#39;#qty&#39;).bind(&#39;blur keyup change&#39;, function() {
    var n = this.value || 0;
    createFields(n)
  });

  function createFields(n) {
    $(&quot;#newFields&quot;).empty(); //Empty the list of fields

    for (var i = 1; i &lt;= n; i++) {
      var fieldWrapper = $(&#39;&lt;div class=&quot;fieldwrapper&quot;/&gt;&#39;); //Create a wrapper
      var name = $(&quot;&lt;p&gt;Subject &quot; + i + &quot;&lt;/p&gt;&quot;); //Create the subject title
      var input = $(&#39;&lt;input name=&quot;Subject&#39; + i + &#39;&quot; placeholder=&quot;Enter Name or Title...&quot; type=&quot;text&quot; required /&gt;&#39;); //Create the input

      fieldWrapper.append(name); //Add the title
      fieldWrapper.append(input); //Add the input
      $(&quot;#newFields&quot;).append(fieldWrapper); //Add the group to the list
    }
  }

});

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

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;input id=&quot;qty&quot; type=&quot;number&quot; /&gt;
&lt;div id=&quot;newFields&quot;&gt;

&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月4日 14:27:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926106.html
匿名

发表评论

匿名网友

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

确定