javascript - 使用 ASP 复选框和 JavaScript 折叠表行

标签 javascript c# html asp.net html-table

我正在使用 ASP 和 JavaScript。我的页面上有一个表格、复选框和文本框字段。选中该复选框后,我想显示第一个 TextBox 并折叠 TextBox2 和 TextBox3 表行。如果复选框未选中,我想向上折叠表行。如何做到这一点?

例如:

enter image description here

这是我尝试过的:

<table>
    <tr>
        <td>
            <asp:CheckBox ID="chkbxUS" runat="server" onchange="validate();" />
        </td>
    </tr>
    <tr id="ParentCountryInfo1">
        <td>
            <asp:TextBox ID="TextBox1" runat="server">Checked Show Me</asp:TextBox>
        </td>
    </tr>
    <tr id="ParentCountryInfo2">
        <td>
            <asp:TextBox ID="TextBox2" runat="server">Un-Checked Show ME</asp:TextBox>
        </td>
    </tr>
    <tr id="ParentCountryInfo3">
        <td>
            <asp:TextBox ID="TextBox3" runat="server">Un-Checked Show ME</asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
             Hello World
        </td>
    </tr>
</table>

<script type="text/javascript">
    function validate() {
        if (document.getElementById('<%=chkbxUS.ClientID%>').checked) {
            document.getElementById('ParentCountryInfo1').style.visibility = 'hidden';
            document.getElementById('ParentCountryInfo2').style.visibility = 'hidden';
            document.getElementById('ParentCountryInfo3').style.visibility = 'hidden';
        } else {
            document.getElementById('ParentCountryInfo1').style.visibility = 'visible';
            document.getElementById('ParentCountryInfo2').style.visibility = 'visible';
            document.getElementById('ParentCountryInfo3').style.visibility = 'visible';
        }
    }
</script>

最佳答案

如果我正确地解决了您的问题,那么以下代码可能会解决您的问题。

<table>
   <tr>
     <td>
        <asp:CheckBox ID="chkbxUS" runat="server" />
     </td>
   </tr>
   <tr id="ParentCountryInfo1">
      <td>
        <asp:TextBox ID="TextBox1" runat="server">Checked Show Me</asp:TextBox>
      </td>
    </tr>
    <tr id="ParentCountryInfo2">
      <td>
        <asp:TextBox ID="TextBox2" runat="server">Un-Checked Show ME</asp:TextBox>
      </td>
    </tr>
    <tr id="ParentCountryInfo3">
      <td>
        <asp:TextBox ID="TextBox3" runat="server">Un-Checked Show ME</asp:TextBox>
      </td>
   </tr>
   <tr>
      <td>
         Hello World
      </td>
   </tr>
 </table>
 <script type="text/javascript">
   $(document).ready(function () {
    $("#chkbxUS").change(function () {

    if ($(this).is(":checked")) {
        $("#TextBox1").show();
        $("#TextBox2").hide();
        $("#TextBox3").hide();
    }
   else
    {
        $("#TextBox1").hide();
        $("#TextBox2").show();
        $("#TextBox3").show();
    }
});


   });

我希望这会有所帮助。

关于javascript - 使用 ASP 复选框和 JavaScript 折叠表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44080362/

相关文章:

javascript - CSS 转换导致向后滚动/闪烁

javascript - 当 Selenium 中自动下拉菜单时,无法触发 Angular ng-change 功能

javascript - 对以整数开头的哈希值进行过滤/排序时出现 Angular $parse 错误

c# - 巢状查询会传回奇怪的结果。 Elasticsearch

c# - "Cannot create instance of WindowsPage"

C# Collection<T>.Remove(T item) 尽管对象数据相等但不起作用

html - 在 XHTML/HTML 中,哪些元素具有语义值,哪些是表示性的,哪些不属于这两个类别?

javascript - Android 上的 Quasar 错误:无法加载位于 ip:port 的网页,因为 net::ERR_ADDRESS_UNREACHABLE

javascript - 如何使用 javascript 为 fontawesome 图标着色?

javascript - AngularJS 服务单元测试在 toEqual 上失败