开发者

Retrieve input id generated by asp.net

I have an input box generated by

 <开发者_如何学C;%= Html.TextBoxFor(model => model.Card.ExpiryDate) %>

which results in the html

<input id="Card_Expiry_Date" name="Card.ExpiryDate" type="text" value="">

I also have javascript function

<script type="text/javascript">
   $().ready(function() {
   $('#Card_Expiry_Date').datepicker({ dateFormat: 'yy-mm-dd' });
</script>

How can I retrieve the input id that will be generated so that I don't have to hard-code the id in my function?


You can specify the ID when creating the textbox:

<%= Html.TextBoxFor(model => model.Card.ExpiryDate, new { id="Card_ExpiryDate" })  %>

Then this will surely work:

 $('#Card_ExpiryDate')...


Thanks Ahmad for you comment, that was what I was looking for!

Client Id for Property (ASP.Net MVC)


I'm not sure for MVC, but in Forms you would inject the textbox id into the html. Something akin to:

$get('<%= TextBox1.ClientID %>')

Link with explanation here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜