简体   繁体   English

如何强制.change jquery事件而无需用户更改文本框中的文本?

[英]how to force .change jquery event without user changing the text in textbox?

I have few textboxes in my asp.net MVC view which show a popup when user clicks on them. 我的asp.net MVC视图中只有几个文本框,当用户单击它们时会显示一个弹出窗口。 The popup also has a textbox, user enters a value in jquery popup and clicks Ok, the selected value is shown in the textbox which was clicked. 弹出窗口还有一个文本框,用户在jquery弹出窗口中输入一个值,然后单击“确定”,所选值显示在单击的文本框中。 I am using textbox.change event on this textbox but it is not fired as the event is fired only If I manually click the textbox. 我正在此文本框上使用textbox.change事件,但不会触发,因为仅当我手动单击文本框时才会触发该事件。

 $(document).ready(function () {


            jQuery("#NumberCalledTo").click(function () {
                displayAlertMessage($(this).val());
            });


 $("#dial-prefix").dialog({
        autoOpen: false,
        modal: true,
        width: 470,
        resizeable: true,
        buttons: {
            "OK": function () {
                var selectedDialPrefixValue = $("#dialprefixPopup").val();
                var selectedPhoneValue = $("#PhoneNumber").val();

                // remove + and space from dialprefix selected on popup
                selectedDialPrefixwithoutPlus = selectedDialPrefixValue.replace(selectedDialPrefixValue.substring(0, 1), " ");
                selectedDialPrefixwithoutPlus = $.trim(selectedDialPrefixwithoutPlus);

                // number must be 10 or 11 digits long
                if (selectedDialPrefixwithoutPlus == "44" && selectedPhoneValue.substring(0, 1) == "0" && selectedPhoneValue.length < 11) {
                    //displayAlertMessage("Invalid number. Must must have 10 digits.");
                    $("#div-uk-error-message").dialog("open");
                    return;
                }
                else if (selectedDialPrefixwithoutPlus == "44" && selectedPhoneValue.length < 10) {
                    //displayAlertMessage("Invalid number. Must must have 9 digits.");
                    $("#div-uk-error-message").dialog("open");
                    return;
                }

                // remove 0 from phone number
                if (selectedPhoneValue.substring(0, 1) == "0") {
                    selectedPhoneValue = selectedPhoneValue.replace(selectedPhoneValue.substring(0, 1), "");
                }

                if (selectedPhoneValue != '') {
                    var selectedDialPrefix = $("#SelecteddialprefixId").val();
                    var selectedPhone = $("#SelectedPhoneId").val();
                    $("#" + selectedDialPrefix).val(selectedDialPrefixValue);
                    $("#" + selectedPhone).val(selectedPhoneValue);                   
                }
                $(this).dialog("close");
            },
            "Cancel": function () {
                $(this).dialog("close");
            }
        }
    });


});

$("#dial-prefix").dialog is opened on textbox click. $(“#dial-prefix”)。dialog在文本框单击时打开。 I can not add any code to OK button function as this popup is being used on different pages and different textboxes. 我无法在“确定”按钮功能中添加任何代码,因为此弹出窗口正在不同的页面和不同的文本框中使用。

Please suggest a solution to it 请提出解决方案

I don't see a change method, so it's hard to decipher the element in question, but if you want to trigger an event, you could just do... 我没有看到一个change方法,因此很难解密有问题的元素,但是如果您要触发事件,则可以...

$('element').trigger('change');

Which will trigger the change event and if any handlers are bound to the event then they will fire. 这将触发change event ,如果将任何handlers are bound to the event则它们将触发。

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

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