Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 需要用我在html上输入的元素形成一个数组_Jquery_Html_Arrays - Fatal编程技术网

Jquery 需要用我在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()”/>中的额外按钮:

我的代码有问题。我试图用来自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()”/>
中的额外
按钮:

<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
});