我遇到過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'
。
這個集合究竟是什麼,它是如何使用的?
在給出的上下文(將輸入附加到DOM的函數)中,它沒有任何作用。密鑰可以是任意確定的字符串。 – zer00ne
我把它運行在jsfiddle和「在服務器上看到的名字」;'什麼都不做。然後將值'key'分配給左側的變量。 – Ayush
註釋名稱在服務器上看到或刪除它,它只是你的字段名稱。沒有其他 –