Jquery 为什么javascript函数不能在MVC5页面中调用
我不熟悉asp.net和javascript。我不明白为什么当我点击按钮时,下面三个javascript函数中的任何一个都没有被调用。有人能帮忙吗Jquery 为什么javascript函数不能在MVC5页面中调用,jquery,asp.net-mvc,razor,Jquery,Asp.net Mvc,Razor,我不熟悉asp.net和javascript。我不明白为什么当我点击按钮时,下面三个javascript函数中的任何一个都没有被调用。有人能帮忙吗 @foreach (var item in Model) { userId = item.Id; <tr> <td> <button class="btn-group" onclick="getElementById('@userId').innerHTML=Add(1
@foreach (var item in Model) {
userId = item.Id;
<tr>
<td>
<button class="btn-group" onclick="getElementById('@userId').innerHTML=Add(1,2)">Reset Password</button>
</td>
<td id="@userId">
</td>
</tr>
}
@foreach(模型中的变量项){
userId=item.Id;
重置密码
}
功能添加(a、b)
{
返回a+b;
}
函数RequestResetPassword()
{
var str=“@userId”;
$.ajax({
类型:“Post”,
url:“/Account/ResetPassword”,
数据:“id=”+“@userId”,
成功:功能(msg){
警报(msg);
}
});
}
$(文档).ready(函数(){
$(“按钮”)。单击(函数(){
警报(“警报”);
//$.post(“/Account/ResetPassword”,
// {
//id:userId,
// },
//功能(数据、状态){
//警报(“数据:+数据+”\n状态:+状态);
// });
});
});
以下代码有效:
<button class="btn-group" onclick="RequestResetPassword('@item.Id')">Reset Password</button>
<script type="text/javascript">
function RequestResetPassword(id)
{
alert("id = " + id);
$.ajax({
type: "Post",
url: "/Account/SendResetPasswordEmail",
//data: "id=" + id,
data: { "id": id },
//dataType: "json",
//contentType: "application/json; charset=utf-8",
success: function (msg) {
alert("msg = " + msg);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
alert("ajax finished")
}
重置密码
函数RequestResetPassword(id)
{
警报(“id=”+id);
$.ajax({
类型:“Post”,
url:“/Account/SendResetPasswordEmail”,
//数据:“id=”+id,
数据:{“id”:id},
//数据类型:“json”,
//contentType:“应用程序/json;字符集=utf-8”,
成功:功能(msg){
警报(“msg=”+msg);
},
错误:函数(xhr、ajaxOptions、thrownError){
警报(xhr.状态);
警报(thrownError);
}
});
警报(“ajax已完成”)
}
按钮是否在表单中?如果是,则需要防止默认表单提交。浏览器控制台中有错误吗?这是否意味着页面加载时会自动调用任何javascript函数,即使您没有单击按钮?如果是,如何避免?
<button class="btn-group" onclick="RequestResetPassword('@item.Id')">Reset Password</button>
<script type="text/javascript">
function RequestResetPassword(id)
{
alert("id = " + id);
$.ajax({
type: "Post",
url: "/Account/SendResetPasswordEmail",
//data: "id=" + id,
data: { "id": id },
//dataType: "json",
//contentType: "application/json; charset=utf-8",
success: function (msg) {
alert("msg = " + msg);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
alert("ajax finished")
}