这个问题困扰了我好久,仍然没解决。这里只记录一下我尝试过的解决方案。
我有一个模态框,上面有一个表单,表单中很多input不提,还有两个按钮:按钮A是预测编号,点击后需要ajax发送post请求,异步获取一些数据,再回显到模态框的某个input中;
$("#predictSampleIdsButton").click(function(){var formData = new FormData();formData.append("order", $("#addOrUpdateForm").serialize());$.ajax({type: "post",url: "/tool/predict",data: formData,processData: false,contentType: false, success: function (json) {// json是后台返回的字符串结果var ids = json.split(", ");for(i = 0; i < ids.length; i++){// 这个函数用来为表单下方的table增加一行,并设置某列的内容为指定值addSubOrderInfo(ids[i]);}document.getElementById("publicDescription").value = json;//$("#addOrUpdateModal").modal('show');},error: $.tool.ajaxError,});
});
按钮B是添加信息,点击后,模态框下部的table中会增加一行。
$("#addButton").click(function () {if (this.name > 0 && index == 0) {index = this.name;}index++;$('#subOrdersTitles').removeClass('hide');var $template = $('#subOrders'),$clone = $template.clone().removeClass('hide').removeAttr('id').attr('id', 'data-book-index'+index).insertBefore($template);// Update the name attributes$clone.find('[name="rowIdx"]').attr('id', 'rowIdx.' + index).end().find('[name="suboid"]').attr('id', 'suboid.' + index).end().find('[name="statement"]').attr('id', 'statement.' + index).end().find('[name="attach"]').attr('id', 'attach.'+index).end().find('[name="removeRowButton"]').attr('pid', 'data-book-index'+index).end();document.getElementById('rowIdx.'+index).innerHTML = index;
});
整个模态框的结构为:
添加订单
**现在的问题是:**当我先点击添加按钮,再填写内容并点击预测按钮后,模态框保持打开状态,“描述”处正常获取信息,每行也正确添加了信息;但当我不点击添加按钮,只填写内容并点击预测按钮后,模态框直接关闭。
backdrop="static" data-keyboard="false"
这两个属性禁止了两种行为:a) 鼠标点击模态框外,关闭模态框 b) 按esc关闭模态框
在ajax的success中加入 $(“#模态框id”).modal(‘show’);
其实也不止这里加了,我也试着在ajax前加,但是,没变化。。还是之前的问题。
先加一个空白行再删除
其实是模拟我添加按钮的操作,结果也不行。。模态框还是自己关闭了。
现在只能先加一个空白行,就放着。这看起来很傻,不过我还没找到解决办法🚬
上一篇:Java中常用API
下一篇:【设计模式】创建者模式