2013-03-27 65 views
0

改變之後的工作我有一個下拉列表和按鈕的頁面,如下:MVC輸入按鈕的onclick不jQuery的

@Html.DropDownList("MovieType") 

<input id="btnBk" type="button" value="Back" onclick="location.href = '/Test/Index/'" /> 

的下拉列表填充控制器:

public ActionResult DDL() 
{ 
    List<SelectListItem> items = new List<SelectListItem>(); 

    items.Add(new SelectListItem { Text = "Action", Value = "0" }); 

    items.Add(new SelectListItem { Text = "Drama", Value = "1" }); 

    items.Add(new SelectListItem { Text = "Comedy", Value = "2", Selected = true }); 

    items.Add(new SelectListItem { Text = "Science Fiction", Value = "3" }); 

    ViewBag.MovieType = items; 

    return View(); 
} 

選擇dropdownlist項目時,我嘗試將onclick按鈕從原始/ Test/Index /更改爲/ Test/Index1:

$(document).ready(function() {    
    $('#MovieType').change(function() { 
     var href = $('#btnBk').attr("onclick"); 
     alert(href); 
     $('#btnBk').attr('onclick', "location.href='/Test/Index1/'"); 
     href = $('#btnBk').attr("onclick"); 
     alert(href); 
    }) 
}); 

警報消息指示onclick值已更新。但是在更改後點擊後退按鈕時,它沒有響應,而是重定向到/ Test/Index1。

有什麼缺失?

由於提前, 本

回答

1

從你的元素刪除onclick屬性,使它像這樣。 充分利用jQuery進行操作。

$(document).ready(function() {    
     $('#btnBk').click(function(){ 
      location.href='/Test/Index/'; 
     }); 

    $('#MovieType').change(function() { 
     $('#btnBk').unbind('click').click(function(){ 
      location.href='/Test/Index1/'; 
     }); 
    }) 
});