jquery - 如何在外部 javascript 文件中从 @Html.TextBoxFor 检索值

标签 jquery asp.net-mvc asp.net-mvc-4 razor

我在创建 View 中使用 MVC 4,我有以下代码

@Html.TextBoxFor(model => model.USER_EMAIL, new { @class = "form-control", id="userModelEmail" })
@Html.ValidationMessageFor(model => model.USER_EMAIL)

我已添加对我的 View 的引用

<script src="~/Contents/Common.js"></script>

common.js 中,我想从 TextBoxFor 获取值。我在 common.js 中添加了以下代码

$("#btnRegister").click(function () {
    debugger;
    var userData = {
        userEmail: document.getElementById('USER_EMAIL').value(),
        userPassWord: document.getElementById('userModelPwd').value(),
        userConfirmPassword: document.getElementById('userModelCfmPwd').value(),
        userMObile: document.getElementById('userModelMob').value(),    
    }
    debugger;
        $.ajax({
            type: "POST",
            url: 'Home/Register',
            data: '{userDetails:' + JSON.stringify(userData) + '}',
            success: function (res) {
                debugger;
                //  alert("data is posted successfully");
                if (res.success == true) {
                    debugger;
                    $('#modalLogin').modal('show');
                   // window.location.href = window.location.href + res.AdminLocation;
                    debugger;
                } else {
                    document.getElementById("lblInvalidUser").style.display = "inline";
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                alert(xhr.statusMessage);
                return false;
            }
        });

        return true;
});

但在 userEmail: document.getElementById('USER_EMAIL').value() 行,USER_EMAIL 显示无法解析,我也尝试过使用 id document.getElementById('userModelEmail').value() 这里也显示了无法解析的 id。

如何解决?

最佳答案

value 是属性属性,而不是 DOM 中的函数。
更改您的代码如下并尝试

   var userData = {
        userEmail: document.getElementById('userModelEmail').value,
        userPassWord: document.getElementById('userModelPwd').value,
        userConfirmPassword: document.getElementById('userModelCfmPwd').value,
        userMObile: document.getElementById('userModelMob').value,    
    }

或使用 jQuery,如下所示

   var userData = {
        userEmail: $('#userModelEmail').val(),
        userPassWord: $('#userModelPwd').val(),
        userConfirmPassword: $('#userModelCfmPwd').val(),
        userMObile: $('#userModelMob').val(),    
    }

关于jquery - 如何在外部 javascript 文件中从 @Html.TextBoxFor 检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27353279/

相关文章:

javascript - 用于开发拖放 Web 应用程序的最佳 javascript 库

asp.net-mvc - “A potentially dangerous Request.Form value was detected from the client”

c# - asp.net MVC 4,标记地点 - 最佳实践(例如酒吧、商店、餐厅)

javascript - 将附加值传递给 Controller ​​方法 - Razor/MVC 5

c# - 几分钟后 Web api 不记名 token 超时

asp.net-mvc - 从mvc4的主页调用区域的 Action

wpf - 在 WPF 应用程序中使用 ASP.NET MVC Controller 操作

javascript - 在 div 单击 backbone.js 中的 URLS

javascript - Onclick 在 IE 中不起作用

javascript - Wordpress 为不同的表格自定义加载更多按钮