[英]How to display span value that is newly added via input text
Am trying to pass span value to a function, but couldn't get value from the span that is being received via input text field. 我试图将范围值传递给函数,但无法从通过输入文本字段接收的范围中获取值。
Below is my code: 下面是我的代码:
HTML HTML
<div id="tags" style="border:none">
<span class="tag" id="4"></span>
<input type="text" id="inptags" value="" placeholder="Add 5 main categories (enter ,)" />
</div>
Javascript 使用Javascript
<script type="text/javascript">
$(function () {
$('#tags input').on('focusout', function () {
var txt = this.value.replace(/[^a-zA-Z0-9\+\-\.\#]/g, '');
if (txt) {
$(this).before('<span class="tag">' + txt.toLowerCase() + '</span>');
}
this.value = "";
}).on('keyup', function (e) {
if (/(188|13)/.test(e.which)) $(this).focusout();
if ($('#tags span').length == 5) {
alert('Reached Maximum!');
document.getElementById('inptags').style.display = 'none';
}
});
$('#tags').on('click', '.tag', function () {
$(this).remove();
document.getElementById('inptags').style.display = 'block';
});
});
</script>
Using above jquery functions, I can enter value in input text and it gets stored as span element. 使用上面的jquery函数,我可以在输入文本中输入值,并将其存储为span元素。 I want to pass the value of the which is being entered on 'enter key' or ',' . 我想传递在'enter key'或','上输入的的值。 How can I do it? 我该怎么做?
I tried using $(this).innerHTML in keyup event, it doesn't work. 我尝试在keyup事件中使用$(this).innerHTML,它不起作用。
EDIT 编辑
In key up event I tried to call a method with the span value as suggested in the answer(trincot) like below: 在按键事件中,我尝试使用answer(trincot)中建议的span值调用方法,如下所示:
.on('keyup', function (e) {
if (/(188|13)/.test(e.which)) $(this).focusout();
if (/(188|13)/.test(addnewrow(this.value))) $(this).focusout();
});
function addnewrow(inputtext)
{
alert('New row is : '+inputtext);
}
The problem with this is, as soon as I keyin the input text, alert gets.. how can I get the alert with the span value only after pressing either 'Enter' or ',' ? 问题是,一旦我输入了输入文本,便会收到警报。.如何仅在按“ Enter”或“,”后才能获得具有跨度值的警报?
Use this.value
like you already do for the focusout
handler: 像focusout
处理程序一样使用this.value
:
if (/(188|13)/.test(this.value)) $(this).focusout();
You maybe want to improve on that regular expression. 您可能想改进该正则表达式。 If you want to add tags as soon as the user enters a comma, semi-colon, hyphen or space then: 如果要在用户输入逗号,分号,连字符或空格后立即添加标签,则:
if (/([,;- ])/.test(this.value)) $(this).focusout();
You can extend the list of characters according to your preference. 您可以根据自己的喜好扩展字符列表。
To respond to the enter key (which does not change the value), you could extend with a test on the keyCode
: 要响应回车键(不改变值),可以对keyCode
进行测试:
if (e.keyCode == 13 || /([,;\- ])/.test(this.value)) $(this).focusout();
You can use keydown
event instead of keyup
like following. 您可以使用keydown
事件代替如下所示的keyup
事件。
$('#tags input').on('focusout', function () {
var txt = this.value.replace(/[^a-zA-Z0-9\+\-\.\#]/g, '');
if (txt) {
$(this).before('<span class="tag">' + txt.toLowerCase() + '</span>');
}
this.value = "";
}).on('keydown', function (e) {
if(e.which==13 || e.which==188){
addnewrow($(this).val());
$(this).val('');
return;
}
});
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.