1

我使用这个函数来填充textfieldJavascript:

function addMsg(text,element_id) {

document.getElementById(element_id).value += text;

}

当人们单击此链接时,这将被激活:

<a onclick="addMsg('THIS IS THE TEXT ADDED','TEXTFIELDNAME'); return false;">CLICK HERE TO FILL THE FIELDS</a>

但是现在我想textfields一键填写3。因此,如果人们单击该链接,则必须将 3 个不同的值添加到 3 个不同的文本字段中。

这可能吗?我该如何解决这个问题?

4

2 回答 2

0

您执行的操作与您在代码中已经使用过的相同。通过获取它们的 IDS 来填充值。

function fillThreeTxtBoxes(text1, text2, text3) {
 document.getElementById('textfieldID1').value = text1;
 document.getElementById('textfieldID2').value = text2;
 document.getElementById('textfieldID3').value = text3;
}

在 jQuery 中,

$('a').on('click', function() {
 $('#textfieldID1').val(value1);
 $('#textfieldID2').val(value2);
 $('#textfieldID3').val(value3);
})
于 2013-09-29T20:15:46.437 回答
0

现场演示

假设您有:

<a onclick="addMsg(['Test 1','Msg 2','333'], 'field'); return false;">CLICK</a>

<input class="field" type="text">
<input class="field" type="text">
<input class="field" type="text">

你的功能:

function addMsg(arr, cls){

  $('.'+ cls).each(function(i){
    this.value = arr[i];
  });

}
于 2013-09-29T20:21:04.060 回答