Javascript 通过web表单输入时操作存储在数组中的数据
我试图在通过web表单输入数据时,处理以字符串形式存储在数组中的数据。我得到了不一致的结果,我不知道为什么。当我通过“for循环”将数据告诉console.log时,它会起作用,但当我尝试从数组中将特定索引记录到console.log时,我会变得未定义 HTML和Javascript如下所示: HTML JS 我粘贴到提交表单中试图操纵的内容:Javascript 通过web表单输入时操作存储在数组中的数据,javascript,arrays,variables,methods,console.log,Javascript,Arrays,Variables,Methods,Console.log,我试图在通过web表单输入数据时,处理以字符串形式存储在数组中的数据。我得到了不一致的结果,我不知道为什么。当我通过“for循环”将数据告诉console.log时,它会起作用,但当我尝试从数组中将特定索引记录到console.log时,我会变得未定义 HTML和Javascript如下所示: HTML JS 我粘贴到提交表单中试图操纵的内容: 代码中有一个错误:tag_数组只有一个元素,即字符串段数组 您不需要使用.push,只需分配给数组: const input='tag1代码中有一个错误
代码中有一个错误:tag_数组只有一个元素,即字符串段数组 您不需要使用.push,只需分配给数组:
const input='tag1代码中有一个错误:tag_数组只有一个元素,即字符串段数组 您不需要使用.push,只需分配给数组:
const input='tag1这似乎是您要寻找的
缺少:tag_array.pushinput.split这似乎是您要查找的内容
缺少:tag_array.pushinput.splitWell,对于第一个元素,tag_数组[1]将未定义,因为该数组仅包含一项,并且它是tag_数组[0]而不是tag_数组[1]。对于第一个元素,tag_数组[1]将未定义,因为该数组仅包含一项,并且它是tag_数组[0]而不是tag_数组[1]
<head>
</head>
<body>
<form id="form">
<label for="html_input">Input Your HTML</label>
<input type="text" id="html_input" name="html_input">
<button type="submit">Submit HTML</button>
</form>
<p id="output"></p>
<script src=sample.js></script>
</body>
const tag_array = [];
function formSubmit(event) {
const input = document.getElementById("html_input").value;
tag_array.push(input.split("<"));
//the below console.log works
console.log(tag_array);
//the below is returning undefined
console.log(`index 1: ${tag_array[1]}`);
//the below for loop and console.log works
for (let i = 0; i < tag_array.length; i++) {
console.log(tag_array[i]);
};
log.textContent = input;
event.preventDefault();
};
const form = document.getElementById('form');
const log = document.getElementById('output');
form.addEventListener('submit', formSubmit);
<body margin="0" border="0" style="color:#555555; font-family: Arial,
Helvetica, sans-serif; font-size: .7em;">
<!---- PRE-HEADER ------>
<p id="preheader" style="display: none; color:#FFFFFF; font-size:1px;">Proud American deals just for you! Today only, take up to $15 off Vera Bradley Military Collection handbags, 20 percent off Columbia PFG Fishing USA Flag caps, USA and military flags and more, all at shopmyexchange.com and in select stores. Shop now!</p>
<!---- /PRE-HEADER ------>
<!-- Email Wrapper Table -->
<table align="center" width="600" cellpadding="0" cellspacing="0" border="0" margin="0" style="-webkit-text-size-adjust:100%; -ms-text-size-adjust:100%;">```