简体   繁体   English

如何使用jquery隐藏和显示来自javascript的div

[英]how to hide and show div from javascript using jquery

        <script type="text/javascript">
            function toggleDiv()
            {
                if (document.getElementById("searchByRegn_ChkBox").checked)
                {
                        $("#showHideDiv2").show();
                          $('#showHideDiv').hide();
                }
                else
                {
                 $('#showHideDiv2').hide();
                   $('#showHideDiv').show();

                }
            }

        </script>


<tr class="TextDarkBlueBOLD">
                        <td  width="20%" align="left">
                            <input type="checkbox"  id="searchByRegn_ChkBox" checked onclick="toggleDiv(); "/>
                            Custom Search
                        </td>
                        <td valign="top">

                        </td>
</tr>
                  <div id="showHideDiv">   <tr class="TextDarkBlueBOLD">
                        <td  width="20%">
                           Regn. No :
                        </td>
                        <td  align="left">
                            <input type="text" id="rNo" name="regPreSerailNumber" size="15" maxlength="50" onkeypress="return numbersonly(this, event)">
                        </td>
                    </tr></div>
                    <div id="showHideDiv2">
                            <tr>
                              <td class=PageHeader14 colspan="2" align="center">Enrolment Search</td>
                            </tr>
                   </div>

When check box will be checked then I want to hide div showHideDiv and show div showHideDiv2 and if unchecked then I want to show div showHideDiv and hide div showHideDiv2. 当复选框将被检查然后我想隐藏div showHideDiv并显示div showHideDiv2并且如果未选中则我想显示div showHideDiv并隐藏div showHideDiv2。 But its not working 但它不起作用

try this, 尝试这个,

 $('#searchByRegn_ChkBox').click(function(){
    if (this.checked){
      $('#showHideDiv2').show();
      $('#showHideDiv').hide();
      // other stuff..............

Try this using length of :checked : 尝试使用以下长度:checked

if jquery lib is used then this way:

if ($(':checked').length > 0){

  ........all other stuff....

or with javascript: 或使用javascript:

function toggleDiv(){
    if (document.getElementById("searchByRegn_ChkBox").checked){
         document.getElementById("showHideDiv2").style.display='block';
         document.getElementById("showHideDiv").style.display = 'none';
    }else{
         document.getElementById("showHideDiv2").style.display='none';
         document.getElementById("showHideDiv").style.display = 'block';
    }
}

I can't see anything intrinsically broken in your code, unless you've failed to include jQuery, in which case there should be errors in your browser's console whenever you click. 除非你没有包含jQuery,否则我在代码中看不到任何内在破坏的东西,在这种情况下,只要你点击,浏览器控制台就会出现错误。

That said, you should avoid DOM0 inline event handlers. 也就是说,你应该避免使用DOM0内联事件处理程序。 They require that the registered handler be a global variable (bad) and don't support lots of nice things (eg bubbling vs capture) that proper DOM3 event handlers can do. 它们要求注册的处理程序是一个全局变量(坏)并且不支持正确的DOM3事件处理程序可以执行的许多好东西(例如冒泡与捕获)。

As you're using jQuery, you can use that to register the event handler too, which will then make testing the checkbox easier. 当您使用jQuery时,您也可以使用它来注册事件处理程序,这样可以更轻松地测试复选框。 You also need to ensure that you wrap your code in a document.ready handler, which should give you this as the entire required code: 您还需要确保将代码包装在document.ready处理程序中,该处理程序应该将此作为完整的必需代码:

$(function() {   // shorthand for document.ready
    $('#searchByRegn_ChkBox').on('click', function() {
        var state = this.checked;
        $('#showHideDiv2').toggle(state);
        $('#showHideDiv').toggle(!state);
    });
});

Note use of the Boolean version of .toggle which does a .show or a .hide depending on the supplied parameter. 注意使用的Boolean.toggle它做了.show.hide根据所提供的参数。

See http://jsfiddle.net/alnitak/5L4PF/ NB - I changed the HTML too because you had an illegal mix of <tr> and <div> elements. 请参阅http://jsfiddle.net/alnitak/5L4PF/注意 - 我也改变了HTML,因为你有非法混合的<tr><div>元素。

You may need to remove the divs from a table layout to achieve the desired result. 您可能需要从表布局中删除div以获得所需的结果。 You can use the change() selector to fire whenever the checkbox is changed. 每当更改复选框时,您都可以使用change()选择器触发。

jsFiddle - http://jsfiddle.net/Zye8f/1/ jsFiddle - http://jsfiddle.net/Zye8f/1/

jquery part: jquery部分:

$(document).ready(function() {
    $('#searchByRegn_ChkBox').change(function() {
        $('#showHideDiv').toggle();
        $('#showHideDiv2').toggle();
    });
});

and the updated html 和更新的HTML

<input type="checkbox"  id="searchByRegn_ChkBox" checked onclick="toggleDiv(); "/>Custom Search
<div id="showHideDiv2">&nbsp;</div>
<div id="showHideDiv">Regn. No :
    <input type="text" id="rNo" name="regPreSerailNumber" size="15" maxlength="50" onkeypress="return numbersonly(this, event)" />
</div>

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

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