日期:2014-05-17  浏览次数:20831 次

jsp页面中,根据影藏条件变化来显示和影藏表格的一行。
如题:
<table>
 <tr id="h1" style="display:none">
   <td></td>
 </tr>
 <tr>
   <td><input type="text" id="nr"></td>
 </tr>
</table>

第一行随着文本框内的内容而影藏或显示,如文本框输入1,则显示;输入2,则影藏。
请问第一行处的style处的display该如何写,才能起到这样的作用?


------解决方案--------------------
<script type="text/javascript">
         $(document).ready(function() {
$("#nr").keyup(function() {
var a = $("#nr").val();
if(a == 1) {
$("#h1").css("display","block");
}
if(a == 2) {
$("#h1").css("display","none");
}
});
})
        </script>
    </head>
    
    <body>
     <table border="1px">
         <tr id="h1" style="display:none">
           <td>jajajaja</td>
         </tr>
         <tr>
           <td><input type="text" id="nr"></td>
         </tr>
        </table>
    </body>


给你个jquery的实现方式
------解决方案--------------------
<!DOCTYPE html>
<html>
<head>
 
<script> 
 
function displayTr(e){
if(e.value=="1"){
document.getElementById("h1").style.display="";
}
if(e.value=="2"){
document.getElementById("h1").style.display="none";
}
}
 
</script>
</head>
<body>
<table>
 <tr id="h1" style="display:''">
   <td>xcsdsd</td>
 </tr>
 <tr>
   <td><input type="text" id="nr" value="" onchange="displayTr(this)"></td>
 </tr>
</table>

</body>
</html>

------解决方案--------------------
<!DOCTYPE html>
<html>
<head>
  
<script> 
 
window.onload=function(){
 if(document.getElementById("nr").value=="1"){
 document.getElementById("h1").style.display="";
 }
 if(document.getElementById("nr").value=="2"){
  document.getElementById("h1").style.display="none";
 }

function displayTr(e){
if(e.value=="1"){
&