javascript - 我有一个 JS 函数,它应该根据 DropDownListFor 中的事件更改来更改 td 的innerHTML,但仅适用于初始加载

标签 javascript asp.net-mvc razor

我有一个 JS 函数,它应该根据 DropDownListFor 的值更改 TD 的innerHTML。它在初始加载时起作用,但当下拉列表再次更改时不起作用。我还有第二个函数,当下拉列表更改时,它应该调用第一个函数...这是我的代码。

查看:

<div class="row">
    <div class="col-md-12" style="overflow-y:scroll">
        <table class="table table-striped table-hover table-bordered">
            <thead>
                <tr>
                    <th>Terminal</th>
                    <th>Command</th>
                    <th>Command Value</th>
                    <th> </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>@Html.DropDownListFor(o => o.TerminalsDDL, Model.TerminalsDDL, new { id = "ddlTerminalID", @class = "form-control" })</td>
                    <td>@Html.DropDownListFor(o => o.TerminalCommandLookupsDDL, Model.TerminalCommandLookupsDDL, new {id = "ddlCommandValue", @class = "form-control" })</td>
                    <td>@Html.TextBoxFor(o => o.UserEnteredTerminalCommands, new { Class = "form-control", Id = "cmdValueValue"})</td>
                    <td> <input id="btnSaveTerminalCommand" type="button"  value="Insert" class="btn btn-primary" /> </td>
                </tr>
                <tr>
                    <td colspan="4" id="helpDescript" onchange="UpdateHelpDescript();">@*@Html.DisplayFor(model => model.HelpDescription)*@</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

JS函数:

 function GetHelpDescriptionFromddlCommandValue() {

    var hd = document.getElementById("helpDescript");
    //var strUser = e.options[e.selectedIndex].text;
    var cmdID = document.getElementById("ddlCommandValue");

    var cmd = cmdID.options[cmdID.selectedIndex].text;

    if (cmd == "UpdateConfig") {
        var helpDescript = "No current help description.";
    }
    if (cmd == "PushLogsToS3") {
        var helpDescript = "No current help description.";
    }
    if (cmd == "PushRecentLogsToS3") {
        var helpDescript = "Command Value is the number of days you wish to retreive.";
    }
    if (cmd == "UnlockCard") {
        var helpDescript = "No current help description.";
    }
    if (cmd == "GetWorkingKey") {
        var helpDescript = "No current help description.";
    }
    if (cmd == "GetNewBuild") {
        var helpDescript = "Pre-Approved S3 URL";
    }
    if (cmd == "ExitApp") {
        var helpDescript = "No current help description."
    }

    return helpDescript;
}

document.getElementById("helpDescript").innerHTML = GetHelpDescriptionFromddlCommandValue();

function UpdateHelpDescript() {
    document.getElementById("helpDescript").innerHTML = GetHelpDescriptionFromddlCommandValue();

}

有人可以解释一下为什么第二个功能在更改时不起作用吗?谢谢!

最佳答案

  1. 将 OnChange 事件放入下拉元素中。
  2. 使用不显眼的 Js(将 js 与 html 分开)。
    例如:

    $(文档).ready(函数(){ $("#helpDescript").html(GetHelpDescriptionFromddlCommandValue()); });

关于javascript - 我有一个 JS 函数,它应该根据 DropDownListFor 中的事件更改来更改 td 的innerHTML,但仅适用于初始加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56261739/

相关文章:

javascript - Crocodoc 文档字体在移动设备上太小

validation - 在 blazor razor 页面中使用 editform 时如何重置自定义验证错误

asp.net-mvc - Razor 中的嵌套布局是否很慢?

javascript - window.location强制更新?

javascript - ajax 调用乱序运行的顺序延迟 promise

如果单击 div 或单击任何标签,则 JavaScript

c# - 继承时 Json 的奇怪行为

c# - 所需的数据注释抛出错误而不是验证客户端

asp.net - Visual Studio 2012 Web Publish 不复制文件

c# - 在 <p> 标记内渲染时删除冗余空格