在文本区域插入json对象
Insert json object in text area
你好,我正在用JSP开发一个小应用程序。
我的想法是在用户在文本区域输入时给出一个JSON元素的引用。
例如:如果JSON包含:
二氧化碳,水,c9O
,在文本区域,用户正在输入一个句子,一旦用户输入一个特殊字符,如@或'或/如果他/她写了"c"字符,我想要一个小下拉显示以c开头的两个元素。用户可以选择元素之后,然后当表单发布时,我想提取那些从JSON中输入的信息。
我猜这就像你开始在Facebook上输入名字一样。
任何想法?提前感谢
EDIT: JS Fiddle
<form action="./Protocol" method="POST">
<textarea rows=5 cols=50></textarea>
<input type="submit"/></form>
$('textarea').textcomplete([{
match: /(^|'s)('w{2,})$/,
search: function (term, callback) {
var words = ['google', 'facebook', 'github', 'microsoft', 'yahoo'];
callback($.map(words, function (word) {
return word.indexOf(term) === 0 ? word : null;
}));
},
replace: function (word) {
return word + ' ';
}}]);
上面的JS Fiddle做了我想要的部分。
我想在这里完成的两件事之一
1. 如果在JSON中,每个单词都有一个ID,比如{"ID": "1","name": "GOOGLE"}
我能得到表单发布时textarea中要发布的ID
2. 或者只是数组索引号,我如何将表单中的值与textarea分开POST
好吧,不管我说了什么,这里有一个基本的例子,教你如何实现这一点(因为我很无聊,但不是很无聊,我要为你做的:)):
HTML<input id="input"/>
<div id="dropdown"></div>
JS
// grab the DOM elements
var input = document.getElementById('input');
var dropdown = document.getElementById('dropdown');
// assign an function to the onkeyup event for your input box
input.onkeyup = search;
// define your data structure
var arr = ['cO2', 'H2O', 'c9O'];
function search() {
// get the content and length of the content
// `this` in this instance refers to the element to which
// we assigned the function
var val = this.value;
var len = val.length;
// filter out the elements from the array that match
// the content, or nothing if the input is empty
dropdown.textContent = arr.filter(function(el) {
return val !=='' ? el.substring(0, len) === val : '';
});
}
演示希望能帮到你。
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 如何为json对象中的段发送array[]
- 将JSON对象传递给angular指令
- 更改JSON对象的结构
- 访问JSON对象内部的数组元素
- 在ejs-partial中对JSON对象进行迭代
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- 访问嵌套JSON对象的键,其中键是动态的
- json对象中缺少对象循环
- 发送json对象或使用express路由呈现视图
- 在play2框架中向json对象添加下拉列表项
- 元素名称上带有短划线 (-) 字符的 Json 对象
- autocomplete不接受源的json对象
- 如何在javascript中创建动态json对象
- 在使用客户端脚本时拾取JSON对象
- 如何通过json对象数组为嵌套对象赋值
- 构造JSON对象