制作多个输入字段值的 javaScript/jQuery 数组
简单的问题我无法弄清楚。我想制作一个输入值的 JavaScript 数组,但无法弄清楚简单的代码。
这是 HTML:
<input class="email" value="[email protected]"></input>
<input class="email" value="[email protected]"></input>
这是尚未正确的 JS/jQuery,它尝试创建一个数组:
$.fn.valuesArr = function()
{
var a = [];
$.each(this, function(i, field){
a.push(field.value);
});
return a;
}
var emailArr=$('.email').valuesArr();
最终,这就是我想要的 emailArr:
emailArr= ["[email protected]", "[email protected]"];
不确定我的 valueArr 函数、任何 JS 和/或 jQuery 有什么问题-基于解决方案将不胜感激!
Simple question i can't figure out. I'd like to make a JavaScript array of input values but can't figure out the simple code.
Here's the HTML:
<input class="email" value="[email protected]"></input>
<input class="email" value="[email protected]"></input>
Here's the not yet correct JS/jQuery which attempts to make an array:
$.fn.valuesArr = function()
{
var a = [];
$.each(this, function(i, field){
a.push(field.value);
});
return a;
}
var emailArr=$('.email').valuesArr();
Ultimately, this is what i'd like the emailArr to be:
emailArr= ["[email protected]", "[email protected]"];
Not sure what is wrong with my valuesArr function, any JS and/or jQuery-based solution would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该获取类 email 的所有元素,然后将其分配给 email 数组。
This should get all the elements of class email it value and then assign it to the email array.
正如帕特所说,原来的剧本是正确的。问题在于如何调用脚本。如果您执行以下任一操作,原始脚本将起作用:
感谢 Pat 和 COLD TOLD 的帮助,
The original script was correct as Pat said. The problem was with how the script was called. The original script will work if you do either of the following:
Thanks to both Pat and COLD TOLD for their help,