2011-12-30 48 views
2

我需要爲Html.TextBox顯示jQuery UI Datepicker插件,我該如何做到這一點?針對Html.TextBox控件的jQuery UI Datepicker

jQuery代碼:

<script> 
    $(function() { 
     $("#datepicker").datepicker(); 
    }); 
</script> 

<div class="demo"> 
    <p>Date: <input type="text" id="datepicker" style = "width:150px"/></p> 
</div> 

上面的代碼給了我在我的MVC視圖中的文本框控件在那裏我可以點擊控制,它會彈出一個日曆控件,它工作得很好,但我想要的是重現了Html.Textbox我稍後在代碼中使用相同的行爲,就像這樣:

<%= Html.TextBox("Date", "", new { style = "width:150px"})%> 

我怎樣才能獲得jQuery UI的日期選擇器插件在上面的代碼行工作?

回答

6

添加class="datepicker"在同一頁面上htmlAttributes

<script> 
$(function() { 
    $(".datepicker").datepicker(); 
}); 
</script> 

<div class="demo"> 
    <p>Date: <input type="text" id="datepicker" style = "width:150px"/></p> 
</div> 
    <%= Html.TextBox("Date", "", new { style = "width:150px", @class = "datepicker"})%> 
+2

你不應該有2組相同的ID。 – 2011-12-30 16:29:20

+0

我以前試過。它不起作用。現在我評論了第一個文本框,然後Html.TextBox開始工作。是否因爲datepicker一次只能使用一個...? – ZVenue 2011-12-30 16:32:50

+0

@Ayman Safadi你真的認爲我相信這個投票嗎? – 2011-12-30 16:33:01