2016-07-05 141 views
0

我正在开发一个IBM项目,由于隐私原因,我无法向您显示确切的内容。jQuery .show().hide()不起作用

我是JS和AngularJS的新手,对jQuery根本不了解。我想在相应的复选框被选中时显示特定的div部分。我使用JS和AngularJS尝试了这一点,但没有结果。请查看代码并请帮助我。此外,如果有任何方式使用JS/AngularJS理清问题(更优选,AngularJS),这将是一个容易得多

代码:

var checkbox1 = $("#check1"); 
 
checkbox1.change(function(event) { 
 
    var checkbox1 = event.target; 
 
    if (checkbox1.checked) { 
 
    $("#userform2").show(); 
 
    } else { 
 
    $("#userform2").hide(); 
 
    } 
 
}); 
 
var checkbox2 = $("#check2"); 
 
checkbox2.change(function(event) { 
 
    var checkbox2 = event.target; 
 
    if (checkbox2.checked) { 
 
    $("#userform4").show(); 
 
    } else { 
 
    $("#userform4").hide(); 
 
    } 
 
}); 
 
var checkbox3 = $("#check3"); 
 
checkbox3.change(function(event) { 
 
    var checkbox3 = event.target; 
 
    if (checkbox3.checked) { 
 
    $("#userform5").show(); 
 
    } else { 
 
    $("#userform5").hide(); 
 
    } 
 
}); 
 
var checkbox4 = $("#check4"); 
 
checkbox4.change(function(event) { 
 
    var checkbox4 = event.target; 
 
    if (checkbox4.checked) { 
 
    $("#userform3").show(); 
 
    } else { 
 
    $("#userform3").hide(); 
 
    } 
 
}); 
 
var checkbox5 = $("#check5"); 
 
checkbox5.change(function(event) { 
 
    var checkbox5 = event.target; 
 
    if (checkbox5.checked) { 
 
    $("#userform6").show(); 
 
    } else { 
 
    $("#userform6").hide(); 
 
    } 
 
}); 
 
var checkbox6 = $("#check6"); 
 
checkbox6.change(function(event) { 
 
    var checkbox6 = event.target; 
 
    if (checkbox6.checked) { 
 
    $("#userform7").show(); 
 
    } else { 
 
    $("#userform7").hide(); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 
 

 
<body id="ibm-com" class="ibm-type"> 
 
    <div id="ibm-top" class="ibm-landing-page"> 
 
    <div id="ibm-content-wrapper"> 
 
     <div class="ibm-columns"> 
 
     <div class="ibm-col-6-1">&nbsp;</div> 
 
     <div class="ibm-col-6-4"> 
 
      <form id="userform1" class="ibm-column-form" method="post" action=""> 
 
      <br /> 
 
      <h2 class="ibm-h2">Random Title 1</h2> 
 
      <p class="ibm-form-elem-grp"> 
 
       <label class="ibm-form-grp-lbl">Type of Request:</label> 
 
       <span class="ibm-input-group"> 
 
\t   <input id="check1" name="requestType1" type="checkbox" /> Checkbox #1 \t \t \t    
 
      \t <br /><input id="check2" name="requestType1" type="checkbox" /> Checkbox #2 
 
        <br /><input id="check3" name="requestType1" type="checkbox" /> Checkbox #3 
 
\t   <br /><input id="check4" name="requestType1" type="checkbox" /> Checkbox #4 
 
\t   <br /><input id="check5" name="requestType1" type="checkbox" /> Checkbox #5 
 
\t   <br /><input id="check6" name="requestType1" type="checkbox" /> Checkbox #6 
 
\t  </span> 
 
      </p> 
 
      <div id="userform2" class="ibm-column-form" hidden> 
 
       <br /> 
 
       <h2 class="ibm-h2">Random Title 2</h2> 
 
       <p> 
 
       Hello from Checkbox #1 
 
       </p> 
 
      </div> 
 
      <div id="userform3" class="ibm-column-form" hidden> 
 
       <br /> 
 
       <h2 class="ibm-h2">Random Title 3</h2> 
 
       <p> 
 
       Hello from Checkbox #4 
 
       </p> 
 
      </div> 
 
      <div id="userform4" class="ibm-column-form" hidden> 
 
       <br /> 
 
       <h2 class="ibm-h2">Random Title 4</h2> 
 
       <p class="ibm-form-elem-grp"> 
 
       Hello from Checkbox #2 
 
       </p> 
 
      </div> 
 
      <div id="userform5" class="ibm-column-form" hidden> 
 
       <br /> 
 
       <h2 class="ibm-h2">Random Title 5</h2> 
 
       <p> 
 
       Hello from Checkbox #3 
 
       </p> 
 
      </div> 
 
      <div id="userform6" class="ibm-column-form" hidden> 
 
       <br /> 
 
       <h2 class="ibm-h2">Random Title 6</h2> 
 
       <p> 
 
       Hello from Checkbox #5 
 
       </p> 
 
      </div> 
 
      <div id="userform7" class="ibm-column-form" hidden> 
 
       <br /> 
 
       <h2 class="ibm-h2">Random Title 7</h2> 
 
       <p> 
 
       Hello from Checkbox #6 
 
       </p> 
 
      </div> 
 
      <div id="userform8" class="ibm-column-form"> 
 
       <br /> 
 
       <h2 class="ibm-h2">Random Title 8</h2> 
 
       <p> 
 
       Bye 
 
       </p> 
 
       <br /> 
 
      </div> 
 
      </form> 
 
     </div> 
 
     <div class="ibm-col-6-1">&nbsp;</div> 
 
     </div> 
 
    </div> 
 
    </div>

+0

它看起来确定你能指望什么? – guradio

+0

可能重复[如何显示/隐藏基于角js中的复选框选择div](http://stackoverflow.com/questions/21748594/how-to-show-hide-a-div-on-这个基础的一个复选框选择在角js) – Weedoze

+4

请阅读这个[mcve]重点在最小 - 有很多重复的代码,这可能存在于您的原始,但不有助于隔离潜在的问题。 –

回答

-5

我相信.change()现在已经死了,它应该是on("change", function(){}),而不是重复所有的代码你可以使用$.each声明。给我5分钟,我会写点东西。

你可以写这样的事情,如果你想这样做jQuery中:

$('input[type="checkbox"]').on('change', function() { 
    var checkboxID = $(this).attr('id'); 
    $('[data-id="' + checkboxID + '"]').toggle(); 
}); 

和所有你需要做的是添加相同的ID上的复选框的形式作为数据-ID。 E.G:

<input id="check6" name="requestType1" type="checkbox" /> 

<div id="userform6" class="ibm-column-form" hidden data-id="check6"> 
    <br /> 
    <h2 class="ibm-h2">Random Title 2</h2> 
     <p> 
      Hello from Checkbox #1 
     </p> 
</div> 

这将节省您不必重复您的代码。

+7

您有什么证据证明'.change'已过时? –

+0

'.change'不适用于动态添加的元素。对于这种用例,推荐使用'.on'或'.bind'。但是如果你有一个静态元素,你可以使用'.change'。作为参考:http://elijahmanor.com/differences-between-jquery-bind-vs-live-vs-delegate-vs-on/ – Rajesh

+0

刚刚检查过文档,我误以为.load,.unload等变化。在jQuery 3.0中。 –

1

下面是如何隐藏/显示链接复选框div的简单示例。

两个div - 每个链接到其复选框。

HTML

<input type="checkbox" id="check1" name="Checkbox1" ng-model="checkbox1" /> 
<input type="checkbox" id="check2" name="Checkbox2" ng-model="checkbox2" /> 

<div ng-show="checkbox1"> 
    <h1>This div is shown thanks to checkbox #1</h1> 
</div> 

<div ng-show="checkbox2"> 
    <h1>This div is shown thanks to checkbox #2</h1> 
</div> 

Working JSFiddle

0

对于一个jQuery的解决方案,这将工作:jsFiddle

$("input[type=checkbox]").on("change", function() { 
    var numericId = $(this).attr("id").replace("check",''); 
    if ($(this).is(":checked")) { 
     $("#userform" + numericId).show(); 
    } else { 
     $("#userform" + numericId).hide(); 
    } 
});