2012-11-26 36 views
2

試圖選擇任何一個單選按鈕,這使得出現在文本框,但pratically它不工作:(。所以這裏是在JSP頁面的HTML。文本沒有出現,而選中的單選按鈕

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<style type="text/css"> 
input[type=text]{ 
    display : none 
} 
</style> 
<script type="text/javascript" language="text/html" src="../jscript/jquery-1.7.2.js"></script> 
<script type="text/javascript" language="javascript" src="../jscript/jquery-1.4.2.min.js"></script> 
<script type="text/javascript"> // I 'm using your 1st jquery whose usage is void of css 
$('input[type=radio]').change(function() { 
    $('input[type=text]').css('display','none'); 
    $(this).closest('tr').find('input[type=text]').css('display','block'); 
}); 
</script> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Test </title> 
</head> 
<body> 
<form action=""> 
<div align="left" style="color:#2D7EE7"> 
<table> 
<tr> 
<td> 
<input type="radio" name="radio" value="college"></td> 
<td>College </td> 
<td><input type="text" name="clg"></td></tr> 
<tr> 
<td><input type="radio" name="radio" value="school"></td> 
<td> School </td> 
<td><input type="text" name="schl"></td> 
</tr> 
</table> 
</div> 
</form> 
</body> 
</html> 

,這裏是這塊的jQuery代碼:

<script type="text/javascript"> 
$('input[type=radio]').change(function() { 
     $('input[type=text]').hide(); 
     $(this).parent().next().show(); 
    }); 
</script> 

和css:

<style type="text/css"> 
input[type=text]{ 
    display : none 
} 
</style> 

但仍這不是worki恩:(:(:(所以任何猜測我要去哪裏錯了,歡迎評論。

回答

3

更改比特到:

  $('input[type=radio]').change(function() { 
       $('input[type=text]').hide(); 
       $(this).closest('tr').find('input[type=text]').show(); 
      }); 

OR

 $(document).ready(function(){ 
      $('input[type=radio]').change(function() { 
       $('input[type=text]').css('display','none'); 
       $(this).closest('tr').find('input[type=text]').css('display','block'); 
      }); 
     }); 
+0

'$('input [type = radio]')'我認爲收音機必須在引號中'$('input [type =「radio」]')'如果IAM錯誤,請糾正我的錯誤 – coolguy

+1

這兩種方式都可以。 ;) –

+0

我必須在我的代碼中添加這個css文本?因爲在jsfiddle中它正在工作,但在我的頁面中它不工作....任何猜測爲什麼? :( –

0

首先嚐試僅包括一個jQuery腳本到頁面(刪除jQuery的1.4.2.min.js)

$(':radio')相當於$('[type=radio]')

$(document).ready(function(){ 
    $(':radio').change(function() { 
     // Hide all input:text in the table  
     $('input:text', $(this).closest("table")).hide(); 
     $(this).parent().nextAll().find('input:text').show(); 
    }); 
}); 
相關問題