【问题标题】:Using .each(), I want to .append() a number value to the value .attr()ibute of an <input> in JQuery使用 .each(),我想 .append() 一个数值到 JQuery 中 <input> 的值 .attr()ibute
【发布时间】:2013-03-07 20:32:13
【问题描述】:

我想在&lt;input&gt; 的值属性中放置用逗号分隔的数字。问题是它会删除它并在那里放置一个新数字,而不是将其附加到属性的末尾。有人可以帮忙吗???

您不会在这个 html 中看到当将某些内容放入 div 时 JQuery UI 构建的内容。列表元素将如下所示:

<li>Doe, John, 12787</li>
<li>Doe, Jane, 9867</li>
<li>etc...

我已经提取了列表元素中的数字,我需要构建一个某种数组,然后将数字数组作为值属性

html:

<div id="teamPool">
    <h1 class="ui-widget-header">Your Branch Team</h1>
    <div class="ui-widget-content">
        <ol>
            <li class="placeholder">Add your items here</li>
        </ol>
    </div>
</div>

<div id="remove"><p>Remove</p></div>
<div id="clear"><p>Clear</p></div>
<div id="submit"><p>Submit</p></div>    

    <!--- Hidden variables --->
<div id="hiddenVariables">
    <cfoutput>
        <input type="hidden" name="uid" value="1" />
        <input id="eids" type="hidden" name="eid" value="" />
    </cfoutput>
</div>

JS:

$('#submit').click(function(){
   $('#teamPool li').each(function() {
      var num = parseInt($(this).text().match(/\d+/)[0], 10);
      var strv = num+' ,';
      $('#eids').attr('value',strv);
   });
});

【问题讨论】:

  • 注意,您应该使用.val() 来获取/设置输入的值。

标签: jquery arrays append each attr


【解决方案1】:

你应该在每个人之外加入他们,只调用一次.attr

$('#submit').click(function(){
   var nums = [];
   $('#teamPool li').each(function() {
      var num = parseInt($(this).text().match(/\d+/)[0], 10);
      nums.push(num);
   });
   var strv = nums.join(', ');
   $('#eids').attr('value',strv);
});

【讨论】:

  • 是的,伙计!很好,谢谢你的工作就像一个魅力!我应该跳出框框思考 LOL
猜你喜欢
  • 2014-07-26
  • 2015-03-13
  • 2011-10-19
  • 2011-09-06
  • 1970-01-01
  • 2012-01-20
  • 2021-08-28
  • 2019-02-27
  • 1970-01-01
相关资源
最近更新 更多