我尝试通过按按钮来调用函数,但是什么也没有发生,并且我在控制台中看到了它。


  未捕获的ReferenceError:未定义AddressInputSet
      在HTMLButtonElement.onclick


我是新手,请帮忙
Asp.NET Core 2.2

<h2 class="text-center">Ваши данные</h2>
<form method="post" asp-controller="Profile" asp-action="UserInfo">
    <div class="form-group">
        <label>Email</label>
        <input class="form-control" type="text" placeholder="@ViewBag.User.Email" readonly />
    </div>
    <div class="form-group">
        <label>Имя</label>
        <input class="form-control" type="text" placeholder="@ViewBag.User.FirstName" readonly />
    </div>
    <div class="form-group">
        <label>Фамилия</label>
        <input class="form-control" type="text" placeholder="@ViewBag.User.LastName" readonly />
    </div>
    <div class="form-group">
        <label>Адресс</label>
        <input class="form-control" id="AddressInput" type="text" placeholder="@ViewBag.User.Address" readonly /><br />
        <button type="button" onclick="AddressInputSet()" class="btn btn-primary" id="buttonSetAddress">Изменить</button>
        <button type="button"onclick="SaveAddress()" class="btn btn-success" id="buttonSaveAddress" hidden>Сохранить</button>
    </div>
</form>
@section Scripts{
    <script type="text/javascript">
    function AddressInputSet() {
        document.getElementById('AddressInput').removeAttribute('readonly');
        document.getElementById('buttonSetAddress').hidden = 'true';
        document.getElementById('buttonSaveAddress').removeAttribute('hidden');
        alert('sdfgdfg');
    };
   function SaveAddress() {
        var data ={
            userId: @ViewBag.User.Id,
            address: document.getElementById('AddressInput').value
        };

        $.ajax({
            type: 'POST',
            url: '/Account/ResetAddress',
            data: data,
            //contentType: "application/x-www-form-urlencoded; charset=utf-8",
            //dataType: JSON,
            success: function () {
                document.getElementById('buttonSaveAddress').hidden = 'true';
            },
            error: function () {
                alert('Error');
            }
        });
    };
    </script>
}

最佳答案

@RenderSection("scripts", required: false)视图中检查您的_Layout。确保它位于<body>标记内,而不位于结束的</body>标记外。

关于javascript - 函数未定义C#,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55848777/

10-17 02:53