简体   繁体   English

动态更改ASP.NET C#中的项目而不回发?

[英]Dynamic change to item in ASP.NET C# without postback?

In ASP.NET C#, I have a checkbox and a text box. 在ASP.NET C#中,我有一个复选框和一个文本框。 I would like to make the text box's visibility dependany upon the checkbox (ie if the box is checked, the text box is visible, and if it is not checked, the text box is hidden), but I would like it done "immediately" as opposed to through a postback. 我想让文本框的可见性取决于复选框(即如果选中该框,文本框是可见的,如果未选中,则文本框被隐藏),但我希望它“立即”完成而不是通过回发。 Is this possible? 这可能吗?

You need to write a client side script in javascript that handles this behavior. 您需要在javascript中编写一个处理此行为的客户端脚本。 If you don't know what's javascript google it. 如果你不知道什么是javascript google它。

With jquery you can do something like: 使用jquery,您可以执行以下操作:

$(document).ready(function() {
  $('.mycheckbox').change(function() {
    $('.mycheckbox').is(':checked') ? $('.mytextbox').show() : $('.mytextbox').hide();
  });
});

where mycheckbox and mytextbox are classes for your inputs. mycheckboxmytextbox是您输入的类。

Or you can use ClientID : 或者您可以使用ClientID

<asp:CheckBox runat="server" ID="cb" />
<asp:TextBox runat="server" ID="txt" />
<script type="text/javascript">
$(document).ready(function() {
  $('#<%= cb.ClientID %>').change(function() {
    $('#<%= cb.ClientID %>').is(':checked') ? $('#<%= txt.ClientID %>').show() : $('#<%= txt.ClientID %>').hide();
  });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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