我有一个HTML页面,我有一个文本框(键入您的文本)和TextArea列表。我需要输入到文本框中,然后单击添加按钮,以便文本框中的任何内容都会发送到我的TextArea列表中。我需要在文本框中输入以下格式。无法在textarea基础上对键和值都使用javascript进行排序?
Name=Value
此文本框将被用户用来快速添加名称值对到该文本框下面的列表中。我们如果我们在上面的文本框中键入Hello=World
,然后单击添加,然后在下面的列表中,应该显示为
Hello=World
如果我们再次在同一文本框中键入ABC=PQR
,然后在下面的列表中说,它应该如此显示,这意味着它应该在原始条目下面添加新的名称值对。
Hello=World
ABC=PQR
但如果语法不正确一样,如果它是不是在名称=值对,那么它应该不添加任何东西到列表,而是显示一个弹出那错的输入格式。名称和值只能包含字母数字字符。现在我有以上所有的东西没有任何问题工作正常。
我还有两个按钮Sort by name
和Sort by value
。一旦我点击这两个按钮中的任何一个,就应该使用名称或值对TextArea列表中的条目进行排序。这可能使用Javascript吗?我尝试添加两种方法sortByKey
和sortByValue
,因为我正在使用拆分,排序和连接方法,但它不像预期的那样工作,并且出现了一些问题。我们的jsfiddle。我需要使用纯HTML和Javascript,我不想使用任何库,因为我想保持简单,因为我仍然在学习。我在这里做什么错?
下面是我的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
<style type="text/css">
#my-text-box {
font-size: 18px;
height: 1.5em;
width: 585px;
}
textarea{
width:585px;
height:300px;
}
.form-section{
overflow:hidden;
width:700px;
}
.fleft{float:left}
.fright{float:left; padding-left:15px;}
.fright button{display:block; margin-bottom:10px;}
</style>
<script language="javascript" type="text/javascript">
document.getElementById('add').onclick = addtext;
function addtext() {
var nameValue = document.getElementById('my-text-box').value;
if (/^([a-zA-Z0-9]+=[a-zA-Z0-9]+)$/.test(nameValue))
document.getElementById('output').textContent += nameValue + '\n';
else
alert('Incorrect Name Value pair format.');
}
document.getElementById('sortbykey').onclick = sortByKey;
function sortByKey() {
var textarea = document.getElementById("output");
textarea.value = textarea.key.split("=").sort().join("\n");
}
document.getElementById('sortbyvalue').onclick = sortByValue;
function sortByValue() {
var textarea = document.getElementById("output");
textarea.value = textarea.value.split("=").sort().join("\n");
}
</script>
</head>
<body>
<h3>Test</h3>
<label for="pair">Name/Value Pair</label></br>
<div class="form-section">
<div class="fleft">
<input type='text' id='my-text-box' value="Name=Value" />
</div>
<div class="fright">
<button type="button" id='add' onclick='addtext()'>Add</button>
</div>
</div>
</br>
</br>
</br>
<label for="pairs">Name/Value Pair List</label></br>
<div class="form-section">
<div class="fleft">
<textarea id='output'></textarea>
</div>
<div class="fright">
<button type="button" id='sortbykey' onclick='sortByKey()'>Sort by name</button>
<button type="button" id='sortbyvalue' onclick='sortByValue()'>Sort by value</button>
</div>
</div>
</body>
</html>
怎么样从你最后一个问题(http://stackoverflow.com/questions/32417786/how-to-add-value-in-a-text-area-from-a-textbox-and-then-sort此不同-它)? – j08691
我编辑我的问题只有添加功能,但我没有从标题中删除它。在这里,我特别关注排序问题,并以某种方式不使用我的代码。 – user1950349
'未捕获的ReferenceError:sortByValue不defined' – Rayon