How to get multiple select value both in javascript and php?
在csdn上找到一篇这样的文章 http://blog.csdn.net/hjzdhr/archive/2009/12/22/5056546.aspx
For PHP, we must use test[] for the select name, but it doesn’t work in javascript. document.form1.test is null.
So we should use document.getElementById(‘test[]‘) or document.form1['test[]‘]. On this way, we are able to get multiple select value in both PHP and JavaScript.
1. <form name="form1">
2. <select name="test[]" multiple="multiple" onchange="alert(document.getElementById('test[]').value);">
3. <option value="one">one</option>
4. <option value="two">two</option>
5. <option value="three">three</option>
6. <option value="four">four</option>
7. <option value="five">five</option>
8. </select>
9. </form>
|
于是 扩展一下javascript如何操作option呢?比如增加或者减少项
1. <select name="txtOptions[]" id="txtOptions" size="10" multiple="multiple">
2. <option value="Foreign Exchange">Foreign Exchange</option>
3. <option value="Share CFDs">Share CFDs</option>
4. <option value="Metal & Commodities">Metal & Commodities</option>
5. <option value="Indices">Indices</option>
6. <option value="Binary Options">Binary Options</option>
7. <option value="Option">Option</option>
8. </select>
9. <input name="txtNewOption" size="20" style="font-size: 100%;" type="\"text\"">
10. <a href="javascript:doAddOption();">Add option</a>
11. <a href="javascript:removeSelected();"><small>Remove option</small></a>
12. <script language="javascript">
14. var arOptions;
15. arOptions = new Array(255);
16.
17. function doAddOption() {
18. var newOption, strText;
19. var txtOptions = document.getElementById('txtOptions');
20. var txtNewOption = document.getElementById('txtNewOption');
21. strText = txtNewOption.value;
22. newnewOption = new Option(strText, strText);
23. var intPosition = txtOptions.length;
24. txtOptions.options[intPosition] = newOption;
25. txtNewOption.value="";
26. }
27. function removeSelected() {
28. var txtOptions = document.getElementById('txtOptions');
29. txtOptions[txtOptions.selectedIndex]= null;
30. }
31. </script>
|