本文介绍了如何仅在字段中的第一个keyup上使keyup()函数起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个keyup()函数,代码在这里

I have created a keyup() function the code is here

$(document).ready(function() {
    var scntDiv = $('#add_words');
    var wordscount = 1;
    $(document).keyup(function(e) {

        var key = (e.keyCode ? e.keyCode : e.which);
        if (key === 32) {
            wordscount++;
            $('.input1').append('<p>stop touching your keyboard</p>');
            $('<div class="line">Word ' + wordscount + '<input type="text" class="input' + wordscount + '" value="' + wordscount + '" /><a class="remScnt">Remove</a></div>').appendTo(scntDiv);
            i++
            return false;
        }
    });
});

<div id="add_words">
    <div class="line">Word 1<input class="input1" type="text" value="1" /></div>
</div>

正常,但是每当我按空格键时,都添加新的输入字段.问题是我无法用空格键入长字(输入很多字段)

which is working fine but whenever I press spacebar adding new input field. the problem is that I wouldn't able to type long words with space(coming a lot of input field)

例如:-当键入我的世界"时显示两个输入字段.实际上,我需要一个额外的字段.

eg:- when type "my hello world" shows two input field. Actually I need one extra field.

我的问题是,是否有任何选项keyup()函数仅在同一字段中首次起作用

my question is that is there any option keyup() function working only on first time in the same field

如果您知道可以帮我吗

推荐答案

我认为这是您追求的目标

I think this is what you serach for

$(document).ready(function() {
    var scntDiv = $('#add_words');
    var wordscount = 1;
$("#add_words").on("keyup","input[type='text']",function(e) { // Set the eventhandler to the inputs

        var key = (e.keyCode ? e.keyCode : e.which);
        if (key === 32) {
            if($(this).attr("data-isused")!="true"){ // Check if THIS textbox have append a new textbox?
            $(this).attr("data-isused","true"); // Mark that this textbox has append a new one
            wordscount++;
            $('.input1').append('<p>stop touching your keyboard</p>');
            $('<div class="line">Word ' + wordscount + '<input type="text"     class="input' + wordscount + '" value="' + wordscount + '" /><a class="remScnt">Remove</a>    </div>').appendTo(scntDiv);
        //i++ Ignore this, couse its not declared
        return false;
        }
    }
});

});

JS Fiddle: http://jsfiddle.net/pURVS/

JS Fiddle:http://jsfiddle.net/pURVS/

这篇关于如何仅在字段中的第一个keyup上使keyup()函数起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:12