Javascript 重新加载partialview后重新执行document.ready
第一次一切正常并且Javascript 重新加载partialview后重新执行document.ready,javascript,c#,jquery,asp.net-mvc,asp.net-mvc-4,Javascript,C#,Jquery,Asp.net Mvc,Asp.net Mvc 4,第一次一切正常并且$(文档).ready在\u EditPhoneNBR.cshtml中调用函数 在$(document).ready中,我动态更改文本框的掩码。在\u EditPhone.cshtml中,当我选择国家/地区下拉列表更改时,我调用ajax方法并重新加载\u EditPhoneNBR.cshtml并返回需要更改文本框掩码的数据 但它不起作用。我能做这件事吗?。我尝试了所有可能的方法,但没有成功 _EditPhone.cshtml @使用(Html.BeginForm(null,nu
$(文档).ready
在\u EditPhoneNBR.cshtml中调用函数
在$(document).ready
中,我动态更改文本框的掩码。在\u EditPhone.cshtml
中,当我选择国家/地区下拉列表更改时,我调用ajax方法并重新加载\u EditPhoneNBR.cshtml
并返回需要更改文本框掩码的数据
但它不起作用。我能做这件事吗?。我尝试了所有可能的方法,但没有成功
_EditPhone.cshtml
@使用(Html.BeginForm(null,null,FormMethod.Post,new{name=“frmPhone”,id=“frmPhone”}))
{
@Label(“Country:,new{@class=“control Label”})
@Html.DropDownListFor(model=>model.CommunicationCountry,
AppCountryCodeList,
“选择”,
new{htmlAttributes=new{@class=“form control”}
)
@Label(“电话号码”,新的{@class=“control Label”})
@Html.Partial(“\u EditPhoneNBR”,模型)
}
//jquery用于用户选择新国家/地区
$(“#通信国家”).change(函数(){
$.ajax({
url:'customer/CountryChange',
键入:“获取”,
数据:{id:$(“#通信国家”).val(),
数据类型:“json”,
contentType:'application/json;charset=utf-8',
cache:false,
成功:功能(数据){
$(“#divPhoneNbContainer”).empty();
$('#divphonenrbcontainer').html(数据);
},
失败:函数(errMsg){
警报(errMsg);
}
});
});
_EditPhoneNBR.cshtml
@Html.TextBox(“phonecountrycode”,Model.phonecountrycode,new{@class=“spe_comm_country”,@Readonly=“Readonly”})
@TextBox(“Space1”,new{@class=“spe_space_10”})
@TextBox(“phoneareacode”,Model.phoneareacode,new{@class=“spe_comm_AreaCode”})
@TextBox(“Space2”,”,new{@class=“spe_space_10”,@readonly=“readonly”})
@TextBox(“phonenumber”,Model.phonenumber,new{@class=“spe_comm_phonenumber”})
@TextBox(“Space1”、“Ext”、new{@class=“spe_space_30”、@readonly=“readonly”})
@TextBox(“phoneextension”,Model.phoneextension,new{@class=“spe_comm_phoneext”})
$(文档).ready(函数(){
var COUNTRY_CODE_format=$(“#PHONE_COUNTRY_CODE_format”).val();
var AREA_CODE_format=$(“#PHONE_AREA_CODE_format”).val();
var NUMBER_format=$(“#电话号码_format”).val();
var EXTENSION_format=$(“#PHONE_EXTENSION_format”).val();
如果(国家代码格式!=“”){
$('phonecountrycode').mask(国家代码格式);
}
如果(区域代码格式!=“”){
$(“#电话区号”).mask(区号格式);
}
如果(数字格式!=“”){
$('#phonenumber')。掩码(数字格式);
}
如果(扩展名格式!=“”){
$(“#电话分机”).mask(分机格式);
}
});
请帮帮我。一种方法是将来自partial的js代码放入函数中,并在Ajax Get成功时调用它。partial中的任何函数都应该在“parent”中可用,这是我自己使用的技术
在局部视图中:
function initEditPhoneNBR() {
var COUNTRY_CODE_format = $("#PHONE_COUNTRY_CODE_format").val();
var AREA_CODE_format = $("#PHONE_AREA_CODE_format").val();
var NUMBER_format = $("#PHONE_NUMBER_format").val();
var EXTENSION_format = $("#PHONE_EXTENSION_format").val();
if (COUNTRY_CODE_format != "") {
$('#phonecountrycode').mask(COUNTRY_CODE_format);
}
if (AREA_CODE_format != "") {
$('#phoneareacode').mask(AREA_CODE_format);
}
if (NUMBER_format != "") {
$('#phonenumber').mask(NUMBER_format);
}
if (EXTENSION_format != ""){
$('#phoneextension').mask(EXTENSION_format);
}
}
$(document).ready(function () {
initEditPhoneNBR();
});
在ajax中,父视图:
$('#CommunicationCountry').change(function(){
$.ajax({
url: 'customer/CountryChange',
type: "Get",
data: { id: $("#CommunicationCountry").val() },
dataType: 'json',
contentType: 'application/json; charset=utf-8',
cache: false,
success: function (data) {
$("#DivPhoneNBRContainer").empty();
$('#DivPhoneNBRContainer').html(data);
initEditPhoneNBR();
},
failure: function (errMsg) {
alert(errMsg);
}
});
});
initEditPhoneNBR();在ajax中,父视图。不开枪。看起来像是在$('#divphonenrbcontainer').html(数据)之后代码>控件没有返回。@James123您的控制器操作正在返回一个PartialView,对吗?是的,返回PartialView(“\u EditPhoneNBR”,memberphone)
和parentview也是partialview。@James123是否没有由partial(\u EditPhoneNBR)定义的模型?如@model.MemberPhone
function initEditPhoneNBR() {
var COUNTRY_CODE_format = $("#PHONE_COUNTRY_CODE_format").val();
var AREA_CODE_format = $("#PHONE_AREA_CODE_format").val();
var NUMBER_format = $("#PHONE_NUMBER_format").val();
var EXTENSION_format = $("#PHONE_EXTENSION_format").val();
if (COUNTRY_CODE_format != "") {
$('#phonecountrycode').mask(COUNTRY_CODE_format);
}
if (AREA_CODE_format != "") {
$('#phoneareacode').mask(AREA_CODE_format);
}
if (NUMBER_format != "") {
$('#phonenumber').mask(NUMBER_format);
}
if (EXTENSION_format != ""){
$('#phoneextension').mask(EXTENSION_format);
}
}
$(document).ready(function () {
initEditPhoneNBR();
});
$('#CommunicationCountry').change(function(){
$.ajax({
url: 'customer/CountryChange',
type: "Get",
data: { id: $("#CommunicationCountry").val() },
dataType: 'json',
contentType: 'application/json; charset=utf-8',
cache: false,
success: function (data) {
$("#DivPhoneNBRContainer").empty();
$('#DivPhoneNBRContainer').html(data);
initEditPhoneNBR();
},
failure: function (errMsg) {
alert(errMsg);
}
});
});