javascript - 页面重新加载时未命中更改功能
问题描述
我有一个cshtml
视图,它在头部声明了一个 js 文档,并在正文中加载了一个部分
<head>
<script src="~/js/Project/doc.js"></script>
</head>
和部分
<div class="panel" id="panelDoc">
<partial name="_docInformation" model=Model.Doc />
</div>
此部分加载具有各种表格和下拉列表的部分视图。doc.js 具有以下功能,它只是根据用户选择的文档类型再次获取页面上的信息,panelDoc
用新信息替换部分中的 html:
$(document).ready(function () {
$('#docType').change(function () {
debugger;
// Get the selected document type from the dropdown via ID
var docType = $('#docType option:selected').val();
var form = {
ProjectID: $('#ProjectID').val(),
DocType: docType
};
$.ajax({
url: '/Project/LoadDocumentPartial',
type: 'POST',
data: form,
success: function (result) {
if (result.success != undefined && !result.success) {
// Show error stuff
}
else {
// Replace the current html in id with new html
$('#panelDoc').html(result);
}
}
});
});
});
这在第一次加载部分时有效,因为我可以更改下拉菜单并点击此功能,用新信息替换内容。但是,如果我再次更改下拉菜单,它永远不会点击此功能。
据我了解,我们将 js 文件包含在父视图中,每次下拉列表更改时都应该点击它?或者我在这里缺少什么?
解决方案
尝试对 javascript 函数的顶部使用不同的语法:
$(document).on("change", "#docType", (function (e) {
e.preventDefault();
e.stopImmediatePropagation();
......
推荐阅读
- c++ - 为什么结构可以隐式转换以匹配函数的参数类型,但如果它是删除了默认值的赋值运算符,则不能?
- ethereum - 每次购买的最大代币价值 - BSC - Solidity
- java - 权限 PROCESS_OUTGOING_CALLS 用于检测被 Android 拒绝的拨出电话
- javascript - 比较javascript中的两个日期对象
- ansible - 在ansible任务中引用字典变量值
- reactjs - 不支持扩展 create-react-app 获取 src/ 之外的相对导入
- python - 如何将 Stanza Word 标记化数据保存在单个 CSV 文件中?
- javascript - 如何在 javascript 中存储玩家猜测并检查字母字符
- ubuntu - 如何连接到使用 vmwares vmrun CLI 命令启动的虚拟机的 GUI?
- c# - 如何为不和谐机器人编写单元测试