简体   繁体   English

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

[英]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.

相关问题 如何根据输入字段的值在span标记中显示文本 - How to display text in a span tag based on the value of input field 追加到通过Angularjs中的输入文本添加的新span元素的数组 - Append to an array for the new span element that is added via input text in Angularjs 如何将新输入的文本包装到以下已经存在的span标签中 - How to wrap the newly input text into the following already-existed span tag 如何在旁边的跨度中显示“数字类型输入”的值 - How to display the value of a “number type input” in a span next to it 在输入字段中输入值后如何显示跨度? 角JS - How to display the span once the value is entered in input field? Angular JS contenteditable div-以跨度包装文本并设置其样式,而不会影响新添加的文本 - contenteditable div - wrap text in span and style it without affecting newly added text 作法:按一下按钮,将html span(文字)更改为任何输入值 - How to: click button, change html span (text) to whatever input value 单击时将跨度文本添加到输入文本值 - Add span text to input text value on click 如何在重复的输入类型文本中添加金额并显示添加金额 - How to add amount in duplicated input type text and display added amount 单击跨度时,如何获取由跨度标签括起来的文本并将其保存为隐藏的输入值? - How can get the text enclosed by a span tag and save it to a hidden input value when I click the span?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM