javascript 如何在用户不更改文本框中的文本的情况下强制 .change jquery 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14423908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to force .change jquery event without user changing the text in textbox?
提问by DotnetSparrow
I have few textboxes in my asp.net MVC view which show a popup when user clicks on them. 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. 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.
我的 asp.net MVC 视图中有几个文本框,当用户单击它们时会显示一个弹出窗口。弹出窗口也有一个文本框,用户在 jquery 弹出窗口中输入一个值并单击“确定”,选定的值显示在单击的文本框中。我在这个文本框上使用 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. I can not add any code to OK button function as this popup is being used on different pages and different textboxes.
$("#dial-prefix").对话框在文本框点击时打开。我无法向 OK 按钮功能添加任何代码,因为此弹出窗口用于不同的页面和不同的文本框。
Please suggest a solution to it
请提出解决方案
回答by Ohgodwhy
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...
我没有看到更改方法,所以很难破译有问题的元素,但是如果你想触发一个事件,你可以这样做......
$('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
话,他们就会开火。