2010-03-12 124 views
5

我是所有ASP.NET MVC的新手,我正在爲我的項目做一些測試。我想問一下如何從html.radiobutton函數中引入一個javascript函數調用。例如,你怎麼會這樣聲明:帶有html.radiobutton的單選按鈕ASP.NET MVC

<input type="radio" name = "Kingdom" value = "All" onclick="GetSelectedItem(this);" checked ="checked" />

與html.radiobutton。我一直在尋找一些文檔,但我真的不明白,我想這與html對象屬性有關,但並不知道語法,我還沒有找到任何示例。

謝謝大家提前:) vikitor

回答

11

定義的屬性的匿名對象。

<%= Html.Radiobutton("Kingdom", 
         "All", 
         true, 
         new { onclick = "GetSelectedItem(this);" }) %> 

或更好的是,應用處理程序不顯眼與JavaScript(例如使用jQuery)。

<%= Html.RadioButton("Kingdom", "All", true) %> 

<script type="text/javascript"> 
    $(function() { 
     $('input[name=Kingdom]').click(function() { 
      GetSelectedItem(this); // or just put the code inline here 
     }); 
    }); 
</script> 
+0

非常感謝!我一直在尋找這一點,並沒有找到我的問題的正確答案。 問候 – vikitor 2010-03-16 10:25:26