2016-11-30 129 views
-1

我想使用ajax修改我的表數據,但onclick()事件不會觸發任何事情。這裏是代碼。 (的onclick()事件被寫在最後div標籤)onclick()不會觸發我的函數

@{ 
    ViewBag.Title = "Edit_Room"; 
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml"; 
} 
<script src="~/Content/Scripts/jquery-3.1.1.js"></script> 
<script src="~/Content/Scripts/edtiRoom.js"></script> 

<h2>Edit_Room</h2> 
<div> 

    <div class="panel panel-default container"> 

     <div class="panel-body"> 
      <h2>Please enter required information</h2> 
       <div class="col-num2"> 
        <div> 
         <h3>Room Number</h3> 
         <select name="roomid" id="roomid"> 
          @foreach (var item in Model) 
          { 
           <option value="@item.Id">@item.Id</option> 
          } 
         </select> 
        </div> 
        <div> 
         <h3>Room Type</h3> 
         <input type="text" id="roomtype" name="type" pattern="[a-zA-Z]+" /> 
        </div> 
        <div> 
         <h3>Price</h3> 
         <input type="tel" id="price" name="price" pattern="^[0-9]*$" /> 
        </div> 
        <div> 
         <button type="button" onclick="editRoomFeatures()" class="btn btn-primary btn-lg"> 
          Update 
         </button> 
        </div> 

       </div> 
     </div> 
    </div> 

</div> 

這裏是我要上按一下按鈕調用函數(寫在edtiRoom.js文件)。

var editRoomFeatures = function() { 
     var roomid = $("#roomid").find('option:selected').text(); 
     var roomtype = $("#roomtype").val(); 
     var roomprice = $("#price").val(); 

     $.ajax({ 
       url: "Admin/Edit_Room_Features/", 
       type: "POST", 
       data: { roomid: roomid, roomtype: roomtype, roomprice: roomprice } 
      }) 
      .done(function() { 
       alert("Success"); 

       //alert(temp); 
       //$("#" + temp).html(partialViewResult); 
      }); 

    }; 

此「editRoomFeatures」將在「Admin」控制器中調用此函數。

public JsonResult Edit_Room_Features(string roomid,string roomtype,string roomprice) 
     { 
     //do something here 
     } 

但是,當我調試我的項目,沒有發生按鈕onclick。

+0

爲什麼不使用適當的事件處理程序而不是'onclick'? – empiric

+0

控制檯中出現任何錯誤? – empiric

+0

嗯,我認爲這種方法是直接 –

回答

1
function editRoomFeatures() { 
     var roomid = $("#roomid").find('option:selected').text(); 
     var roomtype = $("#roomtype").val(); 
     var roomprice = $("#price").val(); 

     $.ajax({ 
       url: "Admin/Edit_Room_Features/", 
       type: "POST", 
       data: { roomid: roomid, roomtype: roomtype, roomprice: roomprice } 
      }) 
      .done(function() { 
       alert("Success"); 

       //alert(temp); 
       //$("#" + temp).html(partialViewResult); 
      }); 

    } 

沒有括號所需

<button type="button" onclick="editRoomFeatures" class="btn 
btn-primary btn-lg"> 
          Update 
         </button> 
+0

試過了,還沒有發生什麼 –

+0

$(document) .ready(function(){$('button')。click(editRoomFeatures);}); – Yaman

0

保持你的函數之外的$(document)。就緒。它會工作。