开发者

get value from select

开发者 https://www.devze.com 2023-03-31 17:20 出处:网络
Wondering how to get the value of selected option. This is the basic scheme of what I need: <select name=\"jumpto\" id=\"jumpto\">

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.

  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.

0

精彩评论

暂无评论...
验证码 换一张
取 消