2015-09-18 83 views
-1

我是新來的asp.net和javascript。我不明白爲什麼當我點擊按鈕時,以下三個JavaScript函數中的任何一個都不會被調用。誰能幫忙?爲什麼javascript函數不能在mvc 5剃鬚刀頁面調用

@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> 
} 

<script type="text/javascript"> 
function Add(a, b) 
{ 
    return a+b; 
} 
function RequestResetPassword() 
{ 
    var str = "@userId"; 
    $.ajax({ 
     type: "Post", 
     url: "/Account/ResetPassword", 
     data: "id=" + "@userId", 
     success: function(msg){ 
      alert(msg); 
     } 
    }); 
} 
$(document).ready(function() { 
    $("button").click(function() { 
     alert("alert"); 
     //$.post("/Account/ResetPassword", 
     // { 
     //  id: userId, 
     // }, 
     // function (data, status) { 
     //  alert("Data: " + data + "\nStatus: " + status); 
     // }); 
    }); 
}); 

+0

是一個表單按鈕?如果需要防止默認表單提交。瀏覽器控制檯中引發的任何錯誤? – charlietfl

+0

是否意味着即使沒有單擊按鈕,頁面加載時也會自動調用任何JavaScript函數?如果是這樣,如何避免它? – Ping

回答

0

下面的代碼工作:

<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") 
}