2014-06-23 31 views
0

我无法触发当前场景中文本的更改。触发文本框中文本的更改

如果我点击按钮,我正在改变文本的内容。那么如何触发由外部事件引起的文本变化。 例如,点击一个按钮,我可以填写文本框中的任何文本,能够粘贴文本和使用鼠标和键盘。在这些所有场景和场景中,我也希望事件被解雇。

我已经尝试了以下内容。

$('#text_id').on('change', function() { 
    alert('This is not trigged when i changed the text'); 
}); 

当文本被改变时应该发出警报。可能有很多可能的来源来改变文本。

这是我创建的Fiddle

在此先感谢。

这是我更新fiddle

回答

1

更新小提琴 - http://jsfiddle.net/upa2A/1/

$(document).ready(function() { 
    $(document).on('click', "#button_id", function() { 
     $('#text_id').val('TExt changed') 
    }); 

    $(document).on('change', "#text_id", function() { 
     alert('This is not trigged when i changed the text'); 
    }); 
}); 

问题在原来的小提琴 -

  • 代码已经在里面$(document).ready()
  • 012后缺少
  • 不正确使用的$.on从问(了解更多关于它的http://api.jquery.com/on/

编辑基于评论 -

更新小提琴 - http://jsfiddle.net/upa2A/4/

$(document).ready(function() { 
    $(document).on('click', "#button_id", function() { 
     $('#text_id').val('TExt changed').change(); 
    }); 

    $(document).on('change', "#text_id", function() { 
     alert('This is not trigged when i changed the text'); 
    }); 
}); 

更多编辑基于评论主题 -

http://api.jquery.com/change“但对于其他元素类型,事件被推迟到元素失去焦点。”

由于当通过按钮点击改变文本框没有被聚焦时,触发不会自动发生。

+0

对不起你的小提琴没有触发文本的变化。你能否检查 – user3354774

+0

这是给我的...你改变了你自己的小提琴还是使用了我给的链接?因为你的小提琴没有添加JQuery库。 –

+0

@ user3354774它也适用于我 –

0

我在jsfiddle中看到了你的代码。你错过了一些括号和分号。否则我们的代码写入。请检查jQuery库代码。

$('#button_id').on('click',function() 
       { 
        $('#text_id').val('TExt changed'); 
       } 
      ); 

$( '#text_id')上( '变',函数(){警报( '当我改变了文本这不是触发');} )。

+0

@SomnathKharat你可以请我建议我在我的小提琴 – user3354774

+0

中犯的错误,请比较我的代码和上面的代码。 – karthick

+0

首先在你的小提琴中选择库代码。放置支架功能() – karthick

0

尝试下面的代码。但为此,你应该使用jQuery。只要文字发生变化就会引发事件。

$('#text_id').on('input propertychange paste', function() { 
    alert('Text Changed'); 
}); 

工作小提琴是here

+0

当文本被其他事件改变时, on keyup ... like like click the button the text is changed that is one of the example .. – user3354774

+0

这将对文本框模糊起作用... – user3354774

+0

@ user3354774这些是所有文本更改事件的基本内容答案。如果你想要实现一个特定的逻辑,你必须使用你的大脑,通过这些基本思想来表达这一点。 –

0

此事件上的文字变化必然提高。如果它没有用,你肯定会有其他人会为此感到高兴。

var txtVal = ''; 
setInterval(function() { 
    if ($("#text_id").val() != txtVal) { 
     txtVal = $("#text_id").val(); 
     alert("Text Changed"); 
    } 
}, 10); 

工作小提琴是here

+0

我从http://stackoverflow.com/questions/17317465/jquery-textbox-change-event-doesnt-fire-until-textbox-loses-focus获取了这段代码。这是一个很好的答案。你请参考。 –