Jump to content

[SOLVED] selects that can't be the same


jdubwelch

Recommended Posts

I have 4 drop down menus that all have the same options, but they're named "F1", "F2", "F3", "F4". Each one needs a different option selected.  I don't want the same option selected from each.

 

I'm not super good at javascript and ideally i'd like the option that i select in one to be removed in the others.  But that's pretty advanced for me to try.  If you want to tackle that, i'd love it. 

 

But, anyways, I figured I'd just check to make sure none of the selectedIndexes equal each other.  However, even when I select all different options, my script still says that one of them equals each other. What's wrong with my method? What will fix it?

 

Is there a better way to do this?

 

// this is within a function that's checking a huge form of data, here's just the part we're dealing with here.

// check to see if any equal each other
if (document.picks.F1.selectedIndex == document.picks.F2.selectedIndex || 
    document.picks.F1.selectedIndex == document.picks.F3.selectedIndex || 
	document.picks.F1.selectedIndex == document.picks.F4.selectedIndex || 
	document.picks.F2.selectedIndex == document.picks.F3.selectedIndex || 
	document.picks.F2.selectedIndex == document.picks.F4.selectedIndex || 
	document.picks.F3.selectedIndex == document.picks.F3.selectedIndex) {

	alert("Sorry, but your selections can't be the same.");
	document.picks.F1.focus(); // i'd really like it  to focus on the problematic box, but
	return false;
}

Link to comment
Share on other sites

try this code out just wrote it  :P

 

 

<head>
<title>Test</title>
<script>
var arrOptionBoxes = new Array("F1","F2","F3","F4");


function fnCheckOptions(f)
{
var arrAlert = {};	
for (i=0;i<arrOptionBoxes.length;i++)
{
	objBox = eval("f."+arrOptionBoxes[i]);
	if (box = fnInArray(arrAlert,objBox.selectedIndex))
	{
		alert (objBox[objBox.selectedIndex].value+" which is selected in "+arrOptionBoxes[i]+" Already selected in " + box);
		return false;
	}
	else 
	{
		arrAlert[arrOptionBoxes[i]] = objBox.selectedIndex;
	}

}
}

function fnInArray(arrAlert,intSelected)
{
for (strKey in arrAlert)
{
	if (arrAlert[strKey] == intSelected)
	{
		return strKey;
	}
}
return false;
}
</script>
</head>
<body>
<form action="" onsubmit="fnCheckOptions(this);return false;">
<select name="F1">
<option value="Test 1">test 1</option>
<option value="Test 2">test 2</option>
<option value="Test 3">test 3</option>
<option value="Test 4">test 4</option>
<option value="Test 5">test 5</option>
<option value="Test 6">test 6</option>
<option value="Test 7">test 7</option>
</select>
<select name="F2">
<option value="Test 1">test 1</option>
<option value="Test 2">test 2</option>
<option value="Test 3">test 3</option>
<option value="Test 4">test 4</option>
<option value="Test 5">test 5</option>
<option value="Test 6">test 6</option>
<option value="Test 7">test 7</option>
</select>
<select name="F3">
<option value="Test 1">test 1</option>
<option value="Test 2">test 2</option>
<option value="Test 3">test 3</option>
<option value="Test 4">test 4</option>
<option value="Test 5">test 5</option>
<option value="Test 6">test 6</option>
<option value="Test 7">test 7</option>
</select>
<select name="F4">
<option value="Test 1">test 1</option>
<option value="Test 2">test 2</option>
<option value="Test 3">test 3</option>
<option value="Test 4">test 4</option>
<option value="Test 5">test 5</option>
<option value="Test 6">test 6</option>
<option value="Test 7">test 7</option>
</select>

<BR><BR>

<input type="submit" value="test">
</body>
</html>

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.