2017-03-13 44 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

相关问题