简体   繁体   中英

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>
</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?

尝试这样:

<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.

  1. window.location is an object, so you should assign it a value instead of calling it as a function, you could use window.open() for that.

  2. .selectedIndex would be the index of the item, not matter what its value is, you are better off using .value for this particular use.

  3. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM