简体   繁体   English

首先不会触发JQuery中的change事件

[英]change event in JQuery doesn't trigger at first

I am quite new to jQuery. 我对jQuery很陌生。 I am using it for one of my WordPress plugin. 我将其用于我的WordPress插件之一。 I have following code 我有以下代码

<script type="text/javascript">
  $(document).ready(function(){
    $("#tmp-preview").html($("#title").val());
    $("#title").change(function(){
      $("#tmp-preview").html(getTweetPreview);
    });
  });
</script>

I want that every time text in title textfield of wordpress new post's page changes, it should reflect into a div. 我希望wordpress新帖子页面的标题文本字段中的文本每次更改时,都应该反映到div中。 But the problem is, that event doesn't trigger on first change. 但是问题是,该事件不会在第一次更改时触发。 I mean, if user write a title for first time and move focus to other part, that title is not updated in div. 我的意思是,如果用户第一次编写标题并将焦点移到其他部分,则该标题不会在div中更新。 But then again if he changes the title, new title does reflect! 但是如果他更改标题,那么新标题确实会反映出来! Why does this happen like this? 为什么会这样发生?

And another thing I would like to know is, if I want that even though the focus in text field is not lost, title text get updated in div on each character's change, do I have to use keydown event or I can do it with change event somehow? 我想知道的另一件事是,如果我希望即使不失去对文本字段的关注,标题文本也会随着每个字符的更改而在div中更新,我是否必须使用keydown事件还是可以通过更改做到这一点?事件不知何故?

PS: Problem isn't with getTweetPreview() function. PS:问题不是getTweetPreview()函数。 I have already tried using a simple string but still same. 我已经尝试过使用一个简单的字符串,但仍然相同。 I believe this is happening due to some other function calls of wordpress as when title is finished editing, it auto save the draft. 我相信这是由于wordpress的其他一些功能调用而发生的,因为在标题完成编辑后,它会自动保存草稿。 Can this be the issue? 这可能是问题吗? How to solve it, if it is? 如果是的话,如何解决?

This may work: 这可能起作用:

$("#title")
    .on(
        "input", 
        function () {
            $("#tmp-preview").html(getTweetPreview); 
        }
    );

Try something like this if everything else fails: 如果其他所有方法都失败,请尝试如下操作:

var updateTweetPreview = function () { // DRY
    $("#tmp-preview").html(getTweetPreview); 
};

$("#title")
    .one( // sic.
        "keydown", 
        function () {
            updateTweetPreview();
            $("#title").on("change", updateTweetPreview);
        }
    );

Please use the on() function for events. 请对事件使用on()函数。 live() is deprecated as of jQuery 1.7 source . 从jQuery 1.7 源开始不推荐使用live()

As for the actual problem, I think that WordPress' auto save is using ajax to load a copy of the data into the form when autosave is complete. 至于实际问题,我认为WordPress的自动保存功能是在自动保存完成后使用ajax将数据副本加载到表单中。 That would cause your code to have it's contents overwritten. 那会导致您的代码覆盖其内容。 Try putting this in: 尝试放入:

<script type="text/javascript">
  $(document).ready(function(){
    $("#tmp-preview").html($("#title").val());
    $("#title").on("change", function(event){
        event.stopImmediatePropagation()
        $("#tmp-preview").html(getTweetPreview());
    });
  });
</script>

Try using the live method 尝试使用实时方法

$("#title").live('change', function(){
  $("#tmp-preview").html(getTweetPreview);
});

For more information on live Click here 有关现场直播的更多信息,请单击此处

Where is gettweetpreview coming from? gettweetpreview来自哪里? Cos this seems to work every time: 因为这似乎每次都有效:

$("#tmp-preview").html($("#title").val());
$("#title").change(function(){
  $("#tmp-preview").html($(this).val());
});

​Working fiddle: http://jsfiddle.net/TNrPZ/ 工作小提琴: http//jsfiddle.net/TNrPZ/

If getTweetPreview is a function, then you should be calling it rather than just referencing it. 如果getTweetPreview是一个函数,那么您应该调用它,而不仅仅是引用它。 The code would look like: 代码如下所示:

$("#title").live('change', function(){
  $("#tmp-preview").html(getTweetPreview());
});

The only difference here is the inclusion of () after getTweetPreview . 唯一的区别是getTweetPreview之后包含()

You can use the keyup event from the jQuery to have the content of your div updated without loosing the focus from the textbox. 您可以使用jQuerykeyup事件来更新div的内容,而不会失去文本框的焦点。

Try this code . 试试这个代码

$("#tmp-preview").html($("#title").val()); $("#title").keyup(function(){ $("#tmp-preview").html($(this).val()); });

I have seen this happen, and what fixed it was to call focus() on the element. 我已经看到了这种情况的发生,并且解决了在元素上调用 focus()问题。 Try making the call to focus() the very last thing done before the event occurs. 尝试在事件发生之前完成对focus()调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM