[英]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.