粘贴时jQuery点击事件不保留文本框值

时间:2022-04-22 23:52:12

I have a textbox with the id "websiteaddress" and a button with the id "getwebsiteinfo". I'm wanting to fire the click event for the button when something is pasted into the textbox, it works, however the textbox loses what was posted in it. Here is my code. Any ideas on how I can get this working?

我有一个id为“websiteaddress”的文本框和一个id为“getwebsiteinfo”的按钮。当某些东西被粘贴到文本框中时,我想要触发按钮的click事件,它可以正常工作,但文本框会丢失其中发布的内容。这是我的代码。关于如何让这个工作的任何想法?

    <script type="text/javascript" charset="UTF-8">
                    $(document).ready(function() {
                        $("input#websiteaddress").bind("paste", function() {
                            $("input#getwebsiteinfo").click();
                            return true;
                        });
                    }); 
                </script>

2 个解决方案

#1


Here is a working example. My textbox does not have the same issue as yours did, it retains the text. I did have an issue with the click event not being able to grab the pasted text, so i added a settimeout function to allow it time to get the new value.

这是一个有效的例子。我的文本框与您的文本框没有相同的问题,它保留了文本。我确实遇到了点击事件无法抓取粘贴文本的问题,所以我添加了一个settimeout函数,以便有时间获取新值。

JSFIDDLE http://jsfiddle.net/seadonk/vuh7qd4q/

   $().ready(function () {
    $("input#getwebsiteinfo").click(function(){
        setTimeout(function(){         
            var websiteaddress = $("input#websiteaddress").val();   
            alert('BUTTON CLICK: websiteaddress');
        });
    });

    $("input#websiteaddress").bind("paste", function () {
        $("input#getwebsiteinfo").click();
    });
});

#2


Adding a setTimeout to the paste event did the trick, like so:

将一个setTimeout添加到粘贴事件就可以了,如下所示:

    $(document).ready(function() {
            $("input#websiteaddress").bind("paste", function() {
                setTimeout(function() { 
                    $("input#getwebsiteinfo").click();
                });
                return true;
            });
        }); 

#1


Here is a working example. My textbox does not have the same issue as yours did, it retains the text. I did have an issue with the click event not being able to grab the pasted text, so i added a settimeout function to allow it time to get the new value.

这是一个有效的例子。我的文本框与您的文本框没有相同的问题,它保留了文本。我确实遇到了点击事件无法抓取粘贴文本的问题,所以我添加了一个settimeout函数,以便有时间获取新值。

JSFIDDLE http://jsfiddle.net/seadonk/vuh7qd4q/

   $().ready(function () {
    $("input#getwebsiteinfo").click(function(){
        setTimeout(function(){         
            var websiteaddress = $("input#websiteaddress").val();   
            alert('BUTTON CLICK: websiteaddress');
        });
    });

    $("input#websiteaddress").bind("paste", function () {
        $("input#getwebsiteinfo").click();
    });
});

#2


Adding a setTimeout to the paste event did the trick, like so:

将一个setTimeout添加到粘贴事件就可以了,如下所示:

    $(document).ready(function() {
            $("input#websiteaddress").bind("paste", function() {
                setTimeout(function() { 
                    $("input#getwebsiteinfo").click();
                });
                return true;
            });
        });