鍍金池/ 問答/數(shù)據(jù)分析&挖掘  HTML5  HTML/ jquery 遍歷數(shù)組賦值的問題?

jquery 遍歷數(shù)組賦值的問題?

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

回答
編輯回答
空痕

inputVals數(shù)組有多少個(gè)元素啊?
沒看懂這個(gè)賦值是怎么賦值.

2018年4月7日 14:05
編輯回答
尕筱澄
/**
 * 好好看下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 也是一個(gè)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() 才對(duì)。
    }
});
2018年8月28日 18:23