A. 怎麼在js中獲得select標簽被選中的值
JS 控制select選中項,代碼如下:
<html>
<script type="text/javascript">
var selectedValue = '<%= request.getAttribute("line")%>';
function changeSelected(){
jsSelectItemByValue(document.getElementById("mySelect"),selectedValue);
}
function jsSelectItemByValue(objSelect,objItemText) {
for(var i=0;i<objSelect.options.length;i++) {
if(objSelect.options[i].value == objItemText) {
objSelect.options[i].selected = true;
break;
}
}
}
</script>
<body onload="changeSelected()">
<select id="mySelect" name="mySelect">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</body>
</html>
B. 怎樣用javascript實現select標簽選中後,重新載入頁面後保存之前選中的選項與顯示效果
你好!
簡單說下思路,具體代碼就不上了~~搜搜就有了~不是很復雜!
使用cookie保存你操作的動作,其實就是將你選中的結果賦值給一個變數,存到你的cookie裡面。
在頁面載入的時候(如onload),判斷cookie是否有這個變數,如果有將它的值取出來並在下拉列表中進行定位。
C. JavaScript怎樣獲取select標簽當前選擇的值呢
對於以下select標簽,獲取當前選擇的值得方式如下:
<select id="test" name="">
<option value="1">text1</option>
<option value="2">text2</option>
</select>
code:
一:javascript原生的方法
1:拿到select對象: var myselect=document.getElementById("test");
2:拿到選中項的索引:var index=myselect.selectedIndex ; // selectedIndex代表的是你所選中項的index
3:拿到選中項options的value: myselect.options[index].value;
4:拿到選中項options的text: myselect.options[index].text;
D. 請問在js的select標簽中,如何在點擊下拉按鈕之後查詢資料庫,然後把查到的值賦值到options中
$.each(data, function (i, item) {
if (item == null) {
return;
}
$("<option></option>")
.val(item["Value"])
.text(item["Text"])
.appendTo($("#purchaser"));
});
data 就是查詢過來的數據
item["Value"]、item["Text"] 下拉項值、文本 具體欄位看內你自己命名容的