Jquery 需要用我在html上输入的元素形成一个数组
我的代码有问题。我试图用来自html的元素组成一个数组 我的html是这样的:Jquery 需要用我在html上输入的元素形成一个数组,jquery,html,arrays,Jquery,Html,Arrays,我的代码有问题。我试图用来自html的元素组成一个数组 我的html是这样的: <h2>Write the name of a friend </h2> <input id="friends" type="text"/> <input type="button" value="Add name" button onclick="enter()"/> 谢谢是的,您的代码工作正常。您只需删除按钮onclick=“enter()”/>中的额外按钮:
<h2>Write the name of a friend </h2>
<input id="friends" type="text"/> <input type="button" value="Add name" button onclick="enter()"/>
谢谢是的,您的代码工作正常。您只需删除
按钮onclick=“enter()”/>
中的额外按钮:
<input type="button" value="Add name" onclick="enter()"/>
jQuery:
myArray = [];
$('input[type="button"]').click(function () {
var friend = $('#friends').val();
myArray.push(friend);
$('#output').text('My Friends: '+ myArray)//output myArray's value
$('#friends').val('');//reset the input field
});
这是问题所在。您不需要一些人发布或读取值吗?您还没有解释什么问题,我不确定代码是否有效。输入名称后如何查看数组?
<h2>Write the name of a friend </h2>
<input id="friends" type="text" />
<input type="button" value="Add name">
<p id="output"></p>
myArray = [];
$('input[type="button"]').click(function () {
var friend = $('#friends').val();
myArray.push(friend);
$('#output').text('My Friends: '+ myArray)//output myArray's value
$('#friends').val('');//reset the input field
});