Javascript 用多个输入字段填充数组
我想使用jQuery用输入字段中的值填充一个数组,其中的类为“seourl”Javascript 用多个输入字段填充数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,我想使用jQuery用输入字段中的值填充一个数组,其中的类为“seourl” <input id="title" class="seourl" name="title" type="text" value="???"> <input id="subtitle" class="seourl" name="title" type="text" value="???"> <input id="subtitle2" class="seourl" name="title" t
<input id="title" class="seourl" name="title" type="text" value="???">
<input id="subtitle" class="seourl" name="title" type="text" value="???">
<input id="subtitle2" class="seourl" name="title" type="text" value="???">
<a id="getFriendlyUrl" href="">get url friendly</a>
如果需要,可以使用jQuery获取.value
return $(this).val();
无论哪种方式,最终都会得到一个值数组。html:
$('.seourl').each(function(ele){
arr_str.push($(ele).val());
});
jsfiddle:。今天是什么时候?.map的返回值不是已经是数组了吗?我更喜欢你的答案:@法布里西奥马特:不,那将是本机Array.prototype.map或$.map。此函数返回一个jQuery对象:-哦,是的,我的错,我在想美元地图+1 =]
return $(this).val();
$('.seourl').each(function(ele){
arr_str.push($(ele).val());
});
$("#getFriendlyUrl").click(function() {
var arr_str = new Array();
$('.seourl').each(function() {
arr_str.push( $(this).val() );
})'
});
<input id="title" class="seourl" name="title" type="text" value="???">
<input id="subtitle" class="seourl" name="title" type="text" value="???">
<input id="subtitle2" class="seourl" name="title" type="text" value="???">
<a id="getFriendlyUrl" href="">get url friendly</a>
$("#getFriendlyUrl").click(function() {
var arr_str = new Array();
$(".seourl").each(function(index, el) {
arr_str[index] = $(el).val();
});
alert(arr_str[0] + arr_str[1] + arr_str[2]);
});
$("#getFriendlyUrl").click(function() {
var arr_str = new Array();
$('.seourl').each(function() {
arr_str.push($(this).attr('value'));
});
alert(arr_str);
});