php如何獲取select multiple的值

php如何獲取select multiple的值
通常常見的表單是單選,文本輸入以及選擇框,但也有多選的,處理多選也還有講究的,咱們看看如下代碼:

<pre>
<?
print_r($_POST);
?>
<form method=post action="">Invest interest<br />
<select name="title[]" id="title" size="6" multiple>
       <option value="Foreign Exchange">Foreign Exchange</option>
       <option value="Share CFDs">Share CFDs</option>
       <option value="Metal & Commodities">Metal & Commodities</option>
       <option value="Indices">Indices</option>
       <option value="Binary Options">Binary Options</option>
       <option value="Option">Option</option>
     </select><br />
<input type="submit" value="提交!">
</form>
</pre>


以上代碼點擊提交後是這樣的:

Array
(
[product] => Array
(
[0] => Foreign Exchange
           [1] => Share CFDs
       )

)


選擇多選就能夠,設置一下顯示幾行


最關鍵的其實就是下面的代碼:
name="title[]" multiple="multiple"



name要設定爲數組標識,不然只能收到最後一個選定的值
size是顯示幾行
multiple是這個形式必選的。

2、若想獲取<select>中有所OPTION值,無論選擇與否,請看下面代碼:

//把參與項目的ID寫入到隱藏域itemid中.
var id = "";
var x = document.getElementByIdx("title");
var len = x.length;
for(var i=0; i<len; i++){
    id += x.options[i].value+",";
}
id = id.substr(0, id.length-1);
document.getElementByIdx("itemId").value = id; php

相關文章
相關標籤/搜索