jQuery:脚本的结果在执行后被删除 – 在后台运行removeData()?

我有一个模态窗口用于更新或添加新的对象Store

此模式具有两个属性的级联下拉列表: DepartmentDistrict

它应该如何工作:

我们首先确定我们是否处于创建或更新Store

如果是一个新的 Store ,模态打开并使用jQuery我们为District下拉列表提供默认值(因为尚未选择任何Department )。

   var wasclicked = 0; var $this = this; $(document).ready(function () { document.getElementById("modalbutton").onclick = function () { //is AddNew Store button is hitted, this var = 1 wasclicked = 1; }; $('#modal-action-store').on('hidden.bs.modal', function () { //global.wasclicked = 0; wasclicked = 0; $(this).removeData('bs.modal'); }); $('#modal-action-store').on('shown.bs.modal', function (e) { console.log($('#DistrictID').length); //if wasclicked equals 1 that means we are in the AddNew Store scenario. if (wasclicked == 1) { //a default value is sent to District dropdownlist var items = "-- Seleccione Distrito --"; $('#DistrictID').html(items); }; }); });  

但是,此时,在添加默认值之后,在几分之一秒后,该值将被删除。

在此处输入图像描述

在此处输入图像描述

我试过的

我注意到当我删除这行代码时:

 $(this).removeData('bs.modal'); 

从上一个脚本,它工作正常,但我需要该代码,以便清除模式中的数据,如果我需要使用模态编辑另一个Store

另外,当我调试项目时,调试器没有停在该行的断点处,所以我不确定为什么它以某种方式在后台执行? 是因为它包含在函数document.ready()吗?

几天来我一直在努力奋斗。 我感谢任何有用的评论。

条件信息:

该项目的在线版本:

有一个在线版本用于调试:

http://plataformafantasypark.azurewebsites.net/

用户:adelgado密码:$ Adelgado33

菜单’Tiendas’ – >’Registro’

调用模态的按钮:

  

模态:

 @model Application.Models.ApplicationviewModels.StoreIndexData @using Application.Models 
@await Html.PartialAsync("_ModalHeader", new ModalHeader { Heading = String.Format("Actualización de Modelo: Tiendas") })

为什么不做(?)之类的事情:

 $("#modalbutton").on('click', function () { //is AddNew Store button is hitted, this var = 1 wasclicked = 1; }); 

我们的想法是手动控制从/ Stores / Create收到的远程内容的加载和插入。 我没有设置测试这个,但尝试一下。

你需要摆脱/注释掉现有的.on('showN.bs.modal')处理程序,你可能不得不去掉调用模态的标签上的href,不确定。

 $('#modal-action-store').on('show.bs.modal', function (e) { if (wasclicked == 1) { // load the form, once it's done loading modify it as needed $('#modal-action-store').find('.modal-content').load('/Stores/Create', function () { $('#DistrictID').html(''); }); } });