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 onchang开发者_开发百科e 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
}
you can write the js script use the jquery , use jquery ,it is very easy to add the event to your element
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.
精彩评论