2017-03-13 43 views
0

我遇到過this nifty little function,用JavaScript添加隱藏的輸入字段到HTML表單。將輸入字段添加到HTML表單中的JavaScript函數不清

下面的代碼:

function addHidden(theForm, key, value) { 
    // Create a hidden input element, and append it to the form: 
    var input = document.createElement('input'); 
    input.type = 'hidden'; 
    input.name = key;'name-as-seen-at-the-server'; 
    input.value = value; 
    theForm.appendChild(input); 
} 

// Form reference: 
var theForm = document.forms['detParameterForm']; 

// Add data: 
addHidden(theForm, 'key-one', 'value'); 
addHidden(theForm, 'another', 'meow'); 
addHidden(theForm, 'foobarz', 'baws'); 

// Submit the form: 
theForm.submit(); 

我不明白的是在input.name = key;'name-as-seen-at-the-server';'name-as-seen-at-the-server'

這個集合究竟是什麼,它是如何使用的?

+1

在給出的上下文(將輸入附加到DOM的函數)中,它沒有任何作用。密鑰可以是任意確定的字符串。 – zer00ne

+1

我把它運行在jsfiddle和「在服務器上看到的名字」;'什麼都不做。然後將值'key'分配給左側的變量。 – Ayush

+1

註釋名稱在服務器上看到或刪除它,它只是你的字段名稱。沒有其他 –

回答

3

這很可能只是描述了key輸入。評論或刪除它,所有應該工作得很好。 將行input.name = key;'name-as-seen-at-the-server';更改爲input.name = key;//'name-as-seen-at-the-server';

+0

因此,這將真正創建名稱爲「key」和值爲「value」的輸入字段,對吧?謝謝!我只是用jsbin試過,它工作得很好 - 謝謝! – SaAtomic

相關問題