Javascript 如何仅在字段中的第一个keyup上生成keyup()函数
我创建了一个keyup()函数,代码在这里Javascript 如何仅在字段中的第一个keyup上生成keyup()函数,javascript,jquery-ui,jquery,Javascript,Jquery Ui,Jquery,我创建了一个keyup()函数,代码在这里 $(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
$(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>
$(文档).ready(函数(){
var scntDiv=$(“#添加单词”);
var-wordscont=1;
$(文档).keyup(函数(e){
var key=(e.keyCode?e.keyCode:e.which);
如果(键===32){
WordScont++;
$('.input1').append('停止触摸键盘');
$('Word'+wordscont+'Remove')。附录(scntDiv);
我++
返回false;
}
});
});
单词1
这是工作正常,但每当我按下空格键添加新的输入字段。
问题是我不能用空格输入长单词(输入字段太多)
例如:-键入“我的hello world”时显示两个输入字段。实际上我需要一个额外的字段
我的问题是,是否有任何选项keyup()
函数仅在同一字段中第一次工作
如果您知道您可以帮助我,请否,但是您可以将类添加到目标输入字段并测试它是否已经在这里,然后什么也不做否,但是您可以将类添加到目标输入字段并测试它是否已经在这里,然后什么也不做您应该使用绑定和解除绑定方法: http://api.jquery.com/unbind/ 例如:
var handler = function() {
alert('The quick brown fox jumps over the lazy dog.');
// unbind handler so the function is not executed again
$('#foo').unbind('click', handler);
};
$('#foo').bind('click', handler);
在处理程序函数中,如果您不想再执行它(在第一次按键后),则调用unbind方法。您应该使用bind和unbind方法: http://api.jquery.com/unbind/ 例如:
var handler = function() {
alert('The quick brown fox jumps over the lazy dog.');
// unbind handler so the function is not executed again
$('#foo').unbind('click', handler);
};
$('#foo').bind('click', handler);
在处理程序函数中,如果您不想再执行它(在第一次按键后),可以调用unbind方法。您可以使用one方法在第一次使用后自动解除事件绑定:
$("#foo").one("keyup", function() {
alert("This will be displayed only once.");
});
首次使用后,您可以使用一种方法自动解除事件绑定:
$("#foo").one("keyup", function() {
alert("This will be displayed only once.");
});
您可以使用
$('#myinput').one('keyup',function(){
// do something
});
您可以使用
$('#myinput').one('keyup',function(){
// do something
});
我不确定你到底想要什么,但是试试这个 使用HTML5
数据。*
了解之前谁使用过该空间
$(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 && $(e.target).data('added')) { // <=== Here
$(e.target).data('added', 'added'); // <=== And Here
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;
}
});
});
$(文档).ready(函数(){
var scntDiv=$(“#添加单词”);
var-wordscont=1;
$(文档).keyup(函数(e){
var key=(e.keyCode?e.keyCode:e.which);
如果(key===32&&$(e.target).data('added'){/我不确定你到底想要什么,但是试试这个
使用HTML5数据。*
了解之前谁使用过该空间
$(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 && $(e.target).data('added')) { // <=== Here
$(e.target).data('added', 'added'); // <=== And Here
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;
}
});
});
$(文档).ready(函数(){
var scntDiv=$(“#添加单词”);
var-wordscont=1;
$(文档).keyup(函数(e){
var key=(e.keyCode?e.keyCode:e.which);
如果(key==32&&$(e.target).data('added'){/我想这就是你想要的
$(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;
}
}
});
$(文档).ready(函数(){
var scntDiv=$(“#添加单词”);
var-wordscont=1;
$(“#添加单词”)。在(“keyup”,“input[type='text']”上,函数(e){//将eventhandler设置为输入
var key=(e.keyCode?e.keyCode:e.which);
如果(键===32){
if($(this).attr(“data isused”)!=“true”){//检查此文本框是否附加了新的文本框?
$(this.attr(“data isused”,“true”);//标记此文本框已附加新文本框
WordScont++;
$('.input1').append('停止触摸键盘');
$(“Word”+wordscont+”我想这就是你想要的
$(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;
}
}
});
$(文档).ready(函数(){
var scntDiv=$(“#添加单词”);
var-wordscont=1;
$(“#添加单词”)。在(“keyup”,“input[type='text']”上,函数(e){//将eventhandler设置为输入
var key=(e.keyCode?e.keyCode:e.which);
如果(键===32){
if($(this).attr(“data isused”)!=“true”){//检查此文本框是否附加了新的文本框?
$(this.attr(“data isused”,“true”);//标记此文本框已附加新文本框
WordScont++;
$('.input1').append('停止触摸键盘');
$('Word'+wordscount+'我是否理解正确:问题是,当您键入“hello world”时,这是可能的。但您不想添加新的输入?“问题是我无法用空格(输入的字段太多)“-诵读困难?请您在英语方面稍加努力!”否则很难确定你到底想要什么…@SimonEdström当我键入“我的hello world”时,将有两个输入字段。实际上,当我开始在字段中键入时,我只需要一个额外的输入。@Stefano我的问题有点改变。希望你现在能理解。对不起,我的英语不好:(我是否理解正确:问题是当你键入“hello world”时,这是可能的。但你不想添加新的输入?“问题是我无法使用空格单词(输入字段很多)”-诵读困难症?请努力学习英语!:)否则很难确定你到底想要什么…@SimonEdström当我键入“我的hello world”时,将有两个输入字段。实际上,当我开始在字段中键入时,我只需要一个额外的输入。@Stefano我的问题有点改变。希望你现在能理解。对不起,我的英语不好:(HTML5数据-*比类更好。@smoula22我正在构建一个马来语词典。此选项允许向词典中添加新词。当有人键入一个词时,将有一个新的文本字段。但这里显示了许多文本字段,因为有些词有很多空间:(我使用了数据。*
在我的回答中,我没有完全理解那个家伙想要什么。HTML5数据-*比类更好。@smoula22我正在建立一个马来语词典。这个选项允许向词典中添加新词。当有人键入一个词时,会有一个新的文本字段。但在这里,它显示了许多文本字段,因为有些词有很多地方:(我用了数据。*
在我的回答中,我没有完全理解那个家伙想要什么