我尝试通过按按钮来调用函数,但是什么也没有发生,并且我在控制台中看到了它。
未捕获的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>
}
答案 0 :(得分:1)
在@RenderSection("scripts", required: false)
视图中检查您的_Layout
。确保它位于<body>
标记内,而不位于结束的</body>
标记外。
答案 1 :(得分:1)
您可以在布局中渲染脚本,例如:
@RenderSection("scripts", false);
OR:而不是内联事件处理程序,请尝试使用一个不引人注目的事件处理程序将事件附加到您的JS代码中:
$('#buttonSetAddress').click(function () {
document.getElementById('AddressInput').removeAttribute('readonly');
document.getElementById('buttonSetAddress').hidden = 'true';
document.getElementById('buttonSaveAddress').removeAttribute('hidden');
alert('sdfgdfg');
});
答案 2 :(得分:0)
可以通过在视图中按部分脚本呈现 javascript 来解决此问题。代码如下
@section scripts{
<script>
$(document).ready(function () {
var allItem = 'test'
console.log(allItem);
});
</script>
}