Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.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
Javascript 如何显示通过输入文本新添加的跨度值_Javascript_Jquery - Fatal编程技术网

Javascript 如何显示通过输入文本新添加的跨度值

Javascript 如何显示通过输入文本新添加的跨度值,javascript,jquery,Javascript,Jquery,我试图将范围值传递给函数,但无法从通过输入文本字段接收的范围中获取值 下面是我的代码: HTML <div id="tags" style="border:none"> <span class="tag" id="4"></span> <input type="text" id="inptags" value="" placeholder="Add 5 main catego

我试图将范围值传递给函数,但无法从通过输入文本字段接收的范围中获取值

下面是我的代码:

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>            

问题是,我一键入输入文本,警报就会出现。。如何仅在按下“回车”或“回车”或“回车”后,才能使用span值获取警报?

使用
此值
,就像您对
聚焦输出处理程序所做的那样:

if (/(188|13)/.test(this.value)) $(this).focusout();
您可能需要改进正则表达式。如果要在用户输入逗号、分号、连字符或空格后立即添加标记,请执行以下操作:

if (/([,;- ])/.test(this.value)) $(this).focusout();
您可以根据自己的喜好扩展字符列表

要响应enter键(该键不改变值),您可以通过对
keyCode
进行测试来扩展:

if (e.keyCode == 13 || /([,;\- ])/.test(this.value)) $(this).focusout();

您可以使用
keydown
事件,而不是像下面这样使用
keydup

$('#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;
                }
            });
$(“#标记输入”).on('focusout',函数(){
var txt=此.value.replace(/[^a-zA-Z0-9\+-\.\\.\\\]/g');
如果(txt){
$(this).before(“”+txt.toLowerCase()+“”);
}
此值为“”;
}).on('keydown',功能(e){
如果(e.which==13 | | e.which==188){
addnewrow($(this.val());
$(this.val(“”);
返回;
}
});

希望这能对您有所帮助。

为什么不使用与
focusout
事件处理程序相同的方法,在该处理程序中使用
this.value
来获取
输入的内容
?不清楚您想要实现什么。是否要捕获输入字段中输入的内容并将其传递给其他函数?是的,Sandeep,我想捕获输入的内容并将其作为参数发送给某些函数您可以使用全局变量我尝试使用全局变量。。但我无法捕获正在输入的内容Hanks@trincot。。我现在会检查它,只要我输入,它仍然显示第一个字符。。如果我只是点击并创建空间,它也会持续显示空间
if (e.keyCode == 13 || /([,;\- ])/.test(this.value)) $(this).focusout();
$('#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;
                }
            });