jquery 遍历数组赋值的问题?

 var tdVals = trs.parents('tr').children('td');
 var inputVals = $('.con-detail input[type=text]');
$(tdVals).each(function(i){
    if(!(i <= 1)){
      inputVals[i-2] = $(this).text();
    }
});

如何将tdVals遍历到的值赋值给inputVals呢?

阅读 2.7k
2 个回答
/**
 * 好好看下jQuery文档吧
 * @see https://api.jquery.com/each/#each-function
 */

var tdVals = trs.parents('tr').children('td');
var inputVals = $('.con-detail input[type=text]');

$(tdVals).each(function(index, td){ // td是DOM, index是索引
    if(!(index <= 1)){
        /**
         * inputVals 也是一个DOM合集,你如果想插入值你得使用下列方法:
         * 1 > inputVals[index].value = "text";
         * 2 > $(inputVals[index]).val("text");
         * @see http://api.jquery.com/val/
         * @see https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/Input
         */ 
        inputVals[i-2].value = $(td).text(); // $(DOM).text() 才对。
    }
});

inputVals数组有多少个元素啊?
没看懂这个赋值是怎么赋值.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题