简体   繁体   中英

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

I'm developing a webapp using ASP.NET MVC and C#. I created my TextBox element using Html.Helper(string, object, object ). Now my problem is, is it possible to add a new html attribute in the already created TextBox element, like a javascript event element?

Because I'm having a trouble using onchange event. 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. Now everytime the I load my page, the changenow will execute, so an added overhead everytime my page load. So I assume that the changenow function will not execute when I create a textbox.

What should I do so that the changenow function will not execute when I load the page?

Please advise.

Many thanks.

You can always use 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM