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/'; 
     }); 
    }) 
});