简体   繁体   English

按钮 onclick 事件不会在第一次点击时触发

[英]Button onclick event not firing on first click

I've checked around and read a couple articles on this issue and tried a few things but nothing seems to be working.我已经检查并阅读了几篇关于这个问题的文章并尝试了一些东西,但似乎没有任何效果。 The issue is when the button is clicked the first time nothing happens.问题是第一次单击按钮时什么也没有发生。 The second time its clicked then the event and ajax request is fired off.第二次点击它时,事件和ajax请求被触发。 In addition to having to click the button twice the ajax data is requested and displayed twice.除了必须两次单击按钮之外,ajax 数据还被请求并显示两次。 Ive seen a few articles that said check the buttons visibility and if it is checking to turn off/on another elements visibility in this case its not.我看过几篇文章,说检查按钮的可见性,以及在这种情况下是否检查关闭/打开另一个元素的可见性。 Additionally I have the onclick of the button calling a method to fire the ajax.另外,我有按钮的 onclick 调用方法来触发 ajax。 Whats strange is when i removed the code from the method and place it in the DOM ready the event fires the first time but my ajax data never returns奇怪的是,当我从方法中删除代码并将其放入 DOM 准备好事件第一次触发但我的 ajax 数据永远不会返回

Button HTML :按钮 HTML :

<input id="btnSearch" onclick="GetInfo();" type="button" value="Find ID" class="button-ffe" />

Here is the click code :这是点击代码:

    function GetInfo() {
    var term = $('#txtUtente').val();

    $('#btnSearch').click(function () {

        var winW = window.innerWidth;
        var winH = window.innerWidth;
        var dialogoverlay = document.getElementById('dialogoverlay');
        dialogoverlay.style.display = "block";
        dialogoverlay.style.height = winH + "px";

        $.ajax({
            url: 'DAL/WebService1.asmx/GetPTSID',
            method: 'post',
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify({ term: term }),
            dataType: 'json',
            success: function (data) {
                document.getElementById('dialogoverlay').style.display = 'none';

                $('#infoFoundID').html(data.d[0]);
                $('#foundInfoMessage, #userInformation').show();
                $('#registerProductInfo').hide();                    
                var partTable = $('#partTable tbody');

                $(data.d).each(function (index, id) {
                    partTable.append('<tr> <td>' + id + '</td><td>' + '<input onclick="GetPartNumber();" id="Button1" type="button" value="Copy Number" />' + '</td></tr>');
                });
            },
            error: function (err) {
                console.log(err);
            }
        });
    });
} //end getinfo

You are attaching the event in the first click, so the code runs like you write.您在第一次单击时附加了事件,因此代码会像您编写的那样运行。

You can remove the first function definition :您可以删除第一个函数定义:

    var term = $('#txtUtente').val();

$('#btnSearch').click(function () {

    var winW = window.innerWidth;
    var winH = window.innerWidth;
    var dialogoverlay = document.getElementById('dialogoverlay');
    dialogoverlay.style.display = "block";
    dialogoverlay.style.height = winH + "px";

    $.ajax({
        url: 'DAL/WebService1.asmx/GetPTSID',
        method: 'post',
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify({ term: term }),
        dataType: 'json',
        success: function (data) {
            document.getElementById('dialogoverlay').style.display = 'none';

            $('#infoFoundID').html(data.d[0]);
            $('#foundInfoMessage, #userInformation').show();
            $('#registerProductInfo').hide();                    
            var partTable = $('#partTable tbody');

            $(data.d).each(function (index, id) {
                partTable.append('<tr> <td>' + id + '</td><td>' + '<input onclick="GetPartNumber();" id="Button1" type="button" value="Copy Number" />' + '</td></tr>');
            });
        },
        error: function (err) {
            console.log(err);
        }
    });
});

So, the first click will run GetInfo() - which, adds a jQuery click listener (but doesn't run it)因此,第一次单击将运行 GetInfo() - 它添加了一个 jQuery 单击侦听器(但不运行它)

then the second click runs the function defined in the click listener - and GetInfo - which adds ANOTHER instance of the jQuery stuff然后第二次单击运行在单击侦听器中定义的函数 - 和 GetInfo - 它添加了 jQuery 东西的另一个实例

then the third click runs the function defined in the click listener, twice - and GetInfo - which adds ANOTHER instance of the jQuery stuff然后第三次点击运行在点击监听器中定义的函数,两次 - 和 GetInfo - 它添加了 jQuery 东西的另一个实例

Your jQuery click function is in your GetInfo() function.您的 jQuery 单击函数在您的GetInfo()函数中。 Change your JS to:将您的 JS 更改为:

var getInfo = {
    init: function() {
        $(document).ready( function() {
            getInfo.click();
        });
    },
    click: function() {
        var term = $('#txtUtente').val();

        $('#btnSearch').click(function () {

            var winW = window.innerWidth;
            var winH = window.innerWidth;
            var dialogoverlay = document.getElementById('dialogoverlay');
            dialogoverlay.style.display = "block";
            dialogoverlay.style.height = winH + "px";

            $.ajax({
                url: 'DAL/WebService1.asmx/GetPTSID',
                method: 'post',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({ term: term }),
                dataType: 'json',
                success: function (data) {
                    document.getElementById('dialogoverlay').style.display = 'none';

                    $('#infoFoundID').html(data.d[0]);
                    $('#foundInfoMessage, #userInformation').show();
                    $('#registerProductInfo').hide();                    
                    var partTable = $('#partTable tbody');

                    $(data.d).each(function (index, id) {
                        partTable.append('<tr> <td>' + id + '</td><td>' + '<input onclick="GetPartNumber();" id="Button1" type="button" value="Copy Number" />' + '</td></tr>');
                    });
                },
                error: function (err) {
                    console.log(err);
                }
            });
        });
    }
};

getInfo.init();

Now you can change your html to:现在您可以将您的 html 更改为:

<input id="btnSearch" type="button" value="Find ID" class="button-ffe" />

That should work.那应该工作。

在 xml 中使用 focusableInTouchMode=false 它对我有用

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

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