【问题标题】:How to build an array with jquery and make input fields如何使用 jquery 构建数组并制作输入字段
【发布时间】:2012-06-18 13:23:49
【问题描述】:

就像我在标题中所说,我需要构建一个数组并更改表单输入字段中的值 代码是:

<div class="row-dp">
    <div class="cel-1">Obiectiv</div>
    <div class="cel-2" id="obiectiv">macarale</div>
    <div class="cel-3">&nbsp;</div>
</div>
<div class="row-dp">
    <div class="cel-1">Orasul selectat</div>
    <div class="cel-2" id="orasulselectat">Bucuresti</div>
    <div class="cel-3">&nbsp;</div>
</div>

我需要从“&lt;div class="cel-2"”中获取 id 作为键和值作为值

一个例子

VAR['obiectiv'] = macarale;
VAR['orasulselectat'] = Bucuresti;

从输入字段中的值转换

<input name="obiectiv" value="macarale" type="text" />

谢谢!

【问题讨论】:

  • 所有input 元素是否已经存在或者是否需要创建?

标签: javascript jquery html arrays forms


【解决方案1】:
$('.cel-2').each(function() {
  $('input[name='+this.id+']').val($(this).text());
});

如果您必须创建 input 元素,请使用:

$('.cel-2').each(function() {
  $('#yourFormNameHere').append($('<input/>').prop({'name':this.id,'type':'hidden'}).val($(this).text()));
});

【讨论】:

  • 它从最后一个 div 的数据中创建带有 value 和 id 的输入
【解决方案2】:
 $('.row-dp').each(function(){
   if($(this).attr('id') && $(this).attr('id')!="")
    $('body').append("<input type='text' id="+$(this).attr('id')+" val="+$(this).text()+">");
 });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 2013-11-01
    • 1970-01-01
    相关资源
    最近更新 更多