ultrasound0000 Posted February 19, 2009 Share Posted February 19, 2009 I have a radio button with three options, let's say, A, B, C. And several drop-down fields (selects), lets say, 1, 2, 3, 4 . . . I want to change the values in select fields based on the radio button choices. So if user selects A on the radio button, then drop-down 1 has one set of values, if user selects option B on the radio button, then drop-down 1 has another set of values, and so on ... I can use JS or PHP to accomplish this. Also, all select values are hard-coded (no db or file). An important thing to note is that on-change the selects call a JS function that does some computations and displays the results on screen right away. So, each time a radio button value changes, it should change the selects' values and also update the computations. Any help is greatly appreciated! Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 23, 2009 Share Posted February 23, 2009 The function "changeModel()" represents your onchange function for the select list. Edit accordingly for your setup: <html> <head> <script type="text/javascript"> var models = new Array(); models['Chrysler'] = ['Pacifica', 'PT Cruiser', 'Sebring']; models['Ford'] = ['Ranger', 'Taurus', 'Mustang']; models['GMC'] = ['Acadia', 'Sierra', 'Yukon']; function changeMake(make) { var modelList = models[make]; changeSelect('model', modelList, modelList); document.getElementById('model').disabled = false; document.getElementById('model').onchange(); } function changeModel(modelValue) { document.getElementById('output').innerHTML = modelValue; return; } function changeSelect(fieldID, newValues, newOptions) { selectField = document.getElementById(fieldID); selectField.options.length = 0; if (newValues && newOptions) { for (i=0; i<newValues.length; i++) { selectField.options[selectField.length] = new Option(newOptions[i], newValues[i]); } } } </script> </head> <body> Make:<br> <input type="radio" name="make" value="Chrysler" onclick="changeMake(this.value);"> Chrysler<br /> <input type="radio" name="make" value="Ford" onclick="changeMake(this.value);"> Ford<br /> <input type="radio" name="make" value="GMC" onclick="changeMake(this.value);"> GMC<br /> <br> Model: <select name="model" id="model" onchange="changeModel(this.value);" disabled="disabled"> <option> -- Select a Make -- </option> </select> <br><br> Output: <span id="output"></span> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.