jdubwelch Posted December 10, 2007 Share Posted December 10, 2007 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 https://forums.phpfreaks.com/topic/80980-solved-selects-that-cant-be-the-same/ Share on other sites More sharing options...
rajivgonsalves Posted December 10, 2007 Share Posted December 10, 2007 try this code out just wrote it <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 https://forums.phpfreaks.com/topic/80980-solved-selects-that-cant-be-the-same/#findComment-410837 Share on other sites More sharing options...
jdubwelch Posted December 10, 2007 Author Share Posted December 10, 2007 You're awesome. Thank you. Link to comment https://forums.phpfreaks.com/topic/80980-solved-selects-that-cant-be-the-same/#findComment-411340 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.