get value from select
Wondering how to get the value of selected option. This is the basic scheme of what I need:
<select name="jumpto" id="jumpto">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option&g开发者_如何转开发t;
</select>
<input type="button" name="go" value="Go!" onClick="window.location('?
showpage='+getElementById('jumpto').selectedIndex);">
Apparently this method doesn't works, since redirect to ?showpage=0, Any idea how I can do?
Try like this:
<input onclick="window.location('?showpage='+document.getElementById('jumpto').value);" type="button" name="go" value="Go!" />
getElementById('jumpto').value
should be
window.location('?showpage='+getElementById('jumpto').options[getElementById('jumpto').selectedIndex].value);
I would recommend to make a functions and place it in onClick= attribute, because getElementById could return null and then an error will be thrown...
HTH
Ivo Stoykov
@Darin Dimitrov's answer fixes the problem, but here is what you had worng.
window.location
is an object, so you should assign it a value instead of calling it as a function, you could usewindow.open()
for that..selectedIndex
would be the index of the item, not matter what its value is, you are better off using.value
for this particular use.Assigning
'?showpage='+getElementById('jumpto').selectedIndex
to window.location would redirect the user to '?showpage=1`, for example, regardless of the current website. You should either add the domain or if this link is to be accessed from only one page, make sure the result would be what you want it exactly.
精彩评论