?
Solved

Jquery: How to disable field (combox, dependent fields)

Posted on 2009-04-18
3
Medium Priority
?
1,799 Views
Last Modified: 2012-06-27
How to disable select list2  after choosing in select list1 option 1.
0
Comment
Question by:AndyPSV
3 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 24177458
You have already some onChange event handler for list1 but you need this statement for disabling list2:
<select name="list1" onChange="this.form.list2.disabled=(this.selectedIndex==1);" >
 
Change selectedIndex compare to zero if you realy mean the first option selected.

Open in new window

0
 
LVL 3

Accepted Solution

by:
hard2u2001 earned 2000 total points
ID: 24182294
Try this example.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- Source is http://abeautifulsite.net/notebook_files/87/demo/ -->
	<head>
		<title>jQuery Select Demo</title>
		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
		<meta name="description" content="This is a demonstration page." />
		<meta name="keywords" content="alert, confirm, prompt, demo" />
		
		
		<!-- Dependencies -->
		<script src="jquery.js" type="text/javascript"></script>
		
		<!-- Example script -->
		<script type="text/javascript">
			
			$(document).ready( function() {
				
				$("#selectox1").bind("change",function(){
					alert("You selected "+this.value);
					if(this.value=="option 1")
					{
						$("#selectox2").attr("disabled",true);
					}
					else
					{
						$("#selectox2").attr("disabled",false);
					}
				});
				$("#selectox1").change();
				
			
			});
			
		</script>
		
		
	</head>
	
	<body>
		
		<h2>Basic Examples</h2>
		
		
		<fieldset>
			<legend>Select Box Demo</legend>
			<pre>
				$("#selectox1").bind("change",function(){
					alert("You selected "+this.value);
					if(this.value=="option 1")
					{
						$("#selectox2").attr("disabled","true");
					}
					else
					{
						$("#selectox2").attr("disabled","false");
					}
				})();
			</pre>
			<p>
				<span>List 1</span>
				<select id="selectox1">
					<option value="option 1">select 1</option>
					<option value="option 2" selected>select 2</option>
					<option value="option 3">select 3</option>
				</select>
			</p>
			<p>
				<span>List 1</span>
				<select id="selectox2">
					<option value="option 1">select 1</option>
					<option value="option 2">select 2</option>
					<option value="option 3">select 3</option>
				</select>
			</p>
		</fieldset>
		<div class="removeNextAd">&nbsp;</div>
		
	</body>
	
</html>

Open in new window

0
 

Author Closing Comment

by:AndyPSV
ID: 31571889
thanks a lot
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question