如何在页面加载时禁用 .change 功能

How to disable .change function when the page loads

本文关键字:change 功能 加载      更新时间:2023-09-26

在我的$(document).ready()函数中,我有一个绑定到集合或单选按钮的.change事件。但是,如果用户提交的表单上出现错误,页面将使用用户选择的值重新加载。这导致单选按钮.change函数在每次页面加载时触发,我不希望它这样做。我将如何禁用此功能?

编辑

这是完整的功能,由于现在中间很混乱,因此之前没有发布,并试图牢记这篇文章的可读性。

仅供参考 - 如果用户提交表单后出现错误,那么我需要页面像用户点击提交按钮时一样加载表单。由于我们根据单选按钮选择显示一组特定的divspan元素,因此我使用了trigger('change')方法。有没有办法检测操作是否在页面加载期间发生?

    $('input[name=TransactionType]').change(function (e) {
        //Clear any messages from previous transaction
        $('.error').text('');
        $('.success').text('');
        //Display fieldset Submit and Cancel(back to search) link
        $('#PageDirection').show();
        $('#AgentInformation').show();
        //Display input fields
        var radioValue = $(this);
        if (radioValue.attr('id') == "Enroll" || (radioValue.attr('id') == "ModT")) {
            //Enable and disable input boxes
            $('span').not('#AllInput').hide();
            $('#AllInput').show();
            $('#PayFrequency').show();
            $('#refNumberInput').show();
            //Set tooltips
            $('#AccountType').removeAttr("title");
            if (radioValue.attr('id') == "Enroll") {
                $('#PaymentFrequency').removeAttr("title");
                $('.req').show();
                $('#refNumberInput').show();
                $('#enrollNote').show();
            } else {
                $('#PaperStatement').show();
                $('#PaymentFrequency').attr("title", "Select the frequency to pay the distributor");
                $('#refNumberInput').show();
            }
        } else if (radioValue.attr('id') == "New") {
            $('span').not('#NewMessage').hide();
            $('#NewMessage').show();
            $('.req').show();
            $('#refNumberInput').show();
        } else if (radioValue.attr('id') == "ModA") {
            $('#AllInput').show();
            $('#AgentIdSpan').show();
            $('.req').show();
            $('#UnenrollMessage').hide();
            $('#NewMessage').hide();
            $('#PayFrequency').hide();
            $('#PaperStatement').hide();
            $('#refNumberInput').show();
            $('#AgentId').attr("title", "Enter the Agent ID to be modified");
        } else if (radioValue.attr('id') == "Clone") {
            $('span').not('#AgentIdSpan').hide();
            $('#AgentIdSpan').show();
            $('.req').show();
            $('#AgentId').attr("title", "Clone EFT onto this Agent ID");
        } else if (radioValue.attr('id') == "Unenroll") {
            $('span').not('#UnenrollMessage').hide();
            $('#UnenrollMessage').show();
            $('.req').show();
            $('#refNumberInput').show();
        }
        if (radioValue.attr('id') != "Clone") {
            $('.alwaysReq').show();
        };
    }).filter(':checked').trigger('change');

如果您不关心自动完成,则可以将autocomplete="off"放在form上。您还可以将当前字段值与其默认值进行比较,以查看它是否由浏览器自动填充。

你可以在jquery ready函数中使用"on"或"delegate"。喜欢这个:

   $(document).delegate("input[name=TransactionType]","change", function(){
        $('.error').text('');
        $('.success').text('');
   });

document.ready通常不会调用更改函数。仅当您手动更改复选框或受其他函数影响的相同事件时,才会调用它,但不会在 document.ready 中调用。

请删除.trigger('change')方法,因为它会在document.ready上触发更改方法