[jQuery]選取器--表單的選取

[jQuery]選取器--表單的選取

語法:

  • :input 泛指所有Form裡面的物件,包括input, textarea, select, button
  • :text 指input形式是text的物件
  • :password 指input形式是password
  • :radio 指input形式是radio
  • :checkbox 指input形式是checkbox
  • 其他功能image, reset, button, file, hidden類似

範例:


<HTML>
	<HEAD>
		<Script Type="text/javascript" src="js/jQuery.js"></Script>
		<Script Type="text/javascript">
			$(document).ready(function(){
				$('#btn1').click(function(){
					$(':input').addClass('st1');
				});
				$('#btn2').click(function(){
					$(':text').addClass('st1');
				});
				$('#btn3').click(function(){
					$(':password').addClass('st1');
				});
				$('#btn5').click(function(){
					$(':checkbox').addClass('st1');
				});
				$('#btn6').click(function(){
					$(':radio').addClass('st1');
				});
				$('#btn7').click(function(){
					$(':image').hide();
				});
			})
		</Script>
		<style type="text/css">
			.st1
			{
				background-Color:pink;
			}
			.st2
			{
				background-Color:Yellow;
			}
			.st3
			{
				background-Color:green;
			}
		</style>
	</HEAD>
	<BODY>
		<FORM action="" method=POST id=form1 name=form1>

			<input type="Button" id="btn1" name="btn1" value="input"><br />
			<input type="Button" id="btn2" name="btn2" value="text"><br />
			<input type="Button" id="btn3" name="btn3" value="Password"><br />
			<input type="Button" id="btn4" name="btn4" value="textarea"><br />
			<input type="Button" id="btn5" name="btn5" value="checkbox"><br />
			<input type="Button" id="btn6" name="btn6" value="radio"><br />
			<input type="Button" id="btn7" name="btn7" value="image"><br />
			<hr />
			text:<input type="text" id="txt1" name="txt1" /><br />
			password:<INPUT type="password" id=password1 name=password1><br />
			checkbox:<INPUT type="checkbox" id=checkbox1 name=checkbox1><br />
			radio:<INPUT type="radio" id=radio1 name=radio1><br />
			<input type="image" id="btnimg" src="topcat.jpg" />
			
		</FORM>
	</BODY>
</HTML>

 


以下是簽名:


Microsoft MVP
Visual Studio and Development Technologies
(2005~Now)