如何将表单的所有元素转换为JavaScript对象?
我想有一些方法从我的表单中自动构建一个JavaScript对象,而不必遍历每个元素.我不想要返回的字符串,$('#formid').serialize();也不想要返回的地图$('#formid').serializeArray();
是否有一种简单的单行方式来获取表单的数据,如果它是以经典的HTML方式提交的话?
例如,在:
<form>
<input type="radio" name="foo" value="1" checked="checked" />
<input type="radio" name="foo" value="0" />
<input name="bar" value="xxx" />
<select name="this">
<option value="hi" selected="selected">Hi</option>
<option value="ho">Ho</option>
</form>
Run Code Online (Sandbox Code Playgroud)
日期:
{
"foo": "1",
"bar": "xxx",
"this": "hi"
}
Run Code Online (Sandbox Code Playgroud)
这样的事情太简单了,因为它没有(正确地)包括textareas,选择,单选按钮和复选框:
$("#form input").each(function () {
data[theFieldName] = theFieldValue;
});
Run Code Online (Sandbox Code Playgroud) 我有一个分类广告网站,在显示广告的页面上,我正在创建一个"向朋友发送提示"表单...
所以任何想要的人都可以将广告提示发送给一些朋友的电子邮件地址.
我猜表单必须提交到php页面吗?
<form name='tip' method='post' action='tip.php'>
Tip somebody: <input name="tip_email" type="text" size="30" onfocus="tip_div(1);" onblur="tip_div(2);"/>
<input type="submit" value="Skicka Tips"/>
<input type="hidden" name="ad_id" />
</form>
Run Code Online (Sandbox Code Playgroud)
提交表单时,页面会重新加载......我不希望这样......
有没有办法让它不重新加载仍然发送邮件?最好没有ajax或jquery ......
谢谢
我想将html输入文件转换为json字符串,如下所示:
var jsonString = JSON.stringify(file);
console.log( file );
console.log( jsonString );
Run Code Online (Sandbox Code Playgroud)
现在,在我的萤火虫中:
File { size=360195, type="image/jpeg", name="xyz.jpg", mehr...}
Object {}
Run Code Online (Sandbox Code Playgroud)
为什么jsonString空?
背景信息:我想用jsonp将文件引用发送到另一个php服务器
附加信息:我想只将文件指针(引用)转换为字符串,通过GET发送.
我试图将HTML表单数据转换为JSON对象,我有这个线程,但我不知道为什么它不适合我.我使用以下代码.
<form id="myform" action="" method="post">
<div class="form-field">
<label for="title">Title</label>
<input name="title" id="title" type="text" value="" size="40" aria-required="true">
</div>
<div class="form-field form-required">
<label for="your-name">Your Name</label>
<input name="yourName" id="yourName" type="text" value="" size="40" aria-required="true">
</div>
<div class="form-field">
<label for="contact-no">Contact No:</label>
<input name="contact" id="contact" type="text" value="" size="40" aria-required="true">
</div>
<div class="form-field">
<label for="description">Description:</label>
<textarea name="description" id="description" rows="1" cols="40" aria-required="true"></textarea>
</div>
<div class="form-field">
<label for="email">Email:</label>
<input name="email" id="email" type="text" value="optional" size="40" aria-required="true">
</div>
<div class="form-field">
<label for="city">City:</label>
<input name="city" id="city" …Run Code Online (Sandbox Code Playgroud) 我正在尝试从表单中提取所有数据并将其转换为查询字符串,然后发布到端点。
这是我到目前为止所拥有的,但是我无法找到一种快速而干净的方法将其转换为例如:key=value&key=value。
let data = Array.from(this.querySelectorAll('input:not([type="submit"]), select, textarea')).map(input => {
let value = '';
switch(input.tagName) {
case 'INPUT':
value = input.value;
break;
case 'SELECT':
value = input[input.selectedIndex].value;
break;
case 'TEXTAREA':
value = input.innerHTML;
break;
}
return {
key: input.name,
value: value
};
});
console.log(data);
// Object.keys(obj).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(obj[k])}`).join('&');
Run Code Online (Sandbox Code Playgroud)
上面的代码创建了一个包含键和值的对象数组。如果能够使用上面注释掉的一个衬垫那就太好了。