[英]Add new html attribute in the created TextBox element using Html Helper
I'm developing a webapp using ASP.NET MVC and C#. 我正在使用ASP.NET MVC和C#开发一个webapp。 I created my TextBox element using Html.Helper(string, object, object ).
我使用Html.Helper(string,object,object)创建了TextBox元素。 Now my problem is, is it possible to add a new html attribute in the already created TextBox element, like a javascript event element?
现在我的问题是,是否可以在已创建的TextBox元素中添加新的html属性,例如javascript事件元素?
Because I'm having a trouble using onchange event. 因为我在使用onchange事件时遇到麻烦。 Please see the code below,
请查看下面的代码,
<% foreach(var md in MD){%>
<tr>
<td>
<div><%= Html.TextBox("tt"+md.id, md.id, new { onchange="changenow('dd"+md.id+"')"})%>
</div>
</td>
</tr>
<%}%>
My changenow
javascript function will update the database using ajax implementation. 我的
changenow
javascript函数将使用ajax实现来更新数据库。 Now everytime the I load my page, the changenow
will execute, so an added overhead everytime my page load. 现在,每次我加载页面时,都会执行
changenow
,因此每次页面加载时都会增加开销。 So I assume that the changenow
function will not execute when I create a textbox. 因此,我假设在创建文本框时将不会执行
changenow
函数。
What should I do so that the changenow
function will not execute when I load the page? 我应该怎么做才能在加载页面时不执行
changenow
函数?
Please advise. 请指教。
Many thanks. 非常感谢。
You can always use jquery. 您可以随时使用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,将事件添加到元素非常容易
If you want to add a property to an existing element using a listener, you can just do: 如果要使用侦听器将属性添加到现有元素,则可以执行以下操作:
<... onchange="this.property='some value'" ...>
There is no need for huge scripts. 不需要庞大的脚本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.