簡體   English   中英

使用HTML Helper在創建的TextBox元素中添加新的html屬性

[英]Add new html attribute in the created TextBox element using Html Helper

我正在使用ASP.NET MVC和C#開發一個webapp。 我使用Html.Helper(string,object,object)創建了TextBox元素。 現在我的問題是,是否可以在已創建的TextBox元素中添加新的html屬性,例如javascript事件元素?

因為我在使用onchange事件時遇到麻煩。 請查看下面的代碼,

<% foreach(var md in MD){%>
<tr>
<td>
<div><%= Html.TextBox("tt"+md.id, md.id, new { onchange="changenow('dd"+md.id+"')"})%>
</div>
</td>
</tr>
<%}%>

我的changenow javascript函數將使用ajax實現來更新數據庫。 現在,每次我加載頁面時,都會執行changenow ,因此每次頁面加載時都會增加開銷。 因此,我假設在創建文本框時將不會執行changenow函數。

我應該怎么做才能在加載頁面時不執行changenow函數?

請指教。

非常感謝。

您可以隨時使用jquery。

$(document).ready(function(){
  $('input[name=tt]').change(function(){
    changenow(this);
  });
});

function changenow(elem){
 $(elem).val(); //this will get you the value
  $(elem).attr('name'); // will get the name attribute
  $(elem).attr('id'); // will get the id attribute
}

您可以使用jquery編寫js腳本,使用jquery,將事件添加到元素非常容易

如果要使用偵聽器將屬性添加到現有元素,則可以執行以下操作:

<... onchange="this.property='some value'" ...>

不需要龐大的腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM