bre*_*req 145 javascript html-select
我有这样的选项菜单:
<form name="AddAndEdit">
<select name="list" id="personlist">
<option value="11">Person1</option>
<option value="27">Person2</option>
<option value="17">Person3</option>
<option value="10">Person4</option>
<option value="7">Person5</option>
<option value="32">Person6</option>
<option value="18">Person7</option>
<option value="29">Person8</option>
<option value="28">Person9</option>
<option value="34">Person10</option>
<option value="12">Person11</option>
<option value="19">Person12</option>
</select>
</form>
Run Code Online (Sandbox Code Playgroud)
现在我想通过使用href更改所选选项.例如:
<a href="javascript:void(0);"
onclick="document.getElementById('personlist').getElementsByTagName('option')[11].selected = 'selected';">change</a>
Run Code Online (Sandbox Code Playgroud)
但我想选择选项value=11 (Person1)
,而不是Person12
.
如何更改此代码?
bre*_*req 184
更改
document.getElementById('personlist').getElementsByTagName('option')[11].selected = 'selected'
Run Code Online (Sandbox Code Playgroud)
至
document.getElementById('personlist').value=Person_ID;
Run Code Online (Sandbox Code Playgroud)
Nee*_*ngh 36
以下是用于处理Selectbox的纯JavaScript代码的所有工具:
更新 - 2018年8月28日| Fiddler DEMO
JavaScript代码:
/**
* Empty Select Box
* @param eid Element ID
* @param value text
* @param text text
* @author Neeraj.Singh
*/
function emptySelectBoxById(eid, value, text) {
document.getElementById(eid).innerHTML = "<option value='" + value + "'>" + text + "</option>";
}
/**
* Reset Select Box
* @param eid Element ID
*/
function resetSelectBoxById(eid) {
document.getElementById(eid).options[0].selected = 'selected';
}
/**
* Set Select Box Selection By Index
* @param eid Element ID
* @param eindx Element Index
*/
function setSelectBoxByIndex(eid, eindx) {
document.getElementById(eid).getElementsByTagName('option')[eindx].selected = 'selected';
//or
document.getElementById(eid).options[eindx].selected = 'selected';
}
/**
* Set Select Box Selection By Value
* @param eid Element ID
* @param eval Element Index
*/
function setSelectBoxByValue(eid, eval) {
document.getElementById(eid).value = eval;
}
/**
* Set Select Box Selection By Text
* @param eid Element ID
* @param eval Element Index
*/
function setSelectBoxByText(eid, etxt) {
var eid = document.getElementById(eid);
for (var i = 0; i < eid.options.length; ++i) {
if (eid.options[i].text === etxt)
eid.options[i].selected = true;
}
}
/**
* Get Select Box Text By ID
* @param eid Element ID
* @return string
*/
function getSelectBoxText(eid) {
return document.getElementById(eid).options[document.getElementById(eid).selectedIndex].text;
}
/**
* Get Select Box Value By ID
* @param eid Element ID
* @return string
*/
function getSelectBoxValue(id) {
return document.getElementById(id).options[document.getElementById(id).selectedIndex].value;
}
Run Code Online (Sandbox Code Playgroud)
Nic*_*ick 24
我相信博客发布JavaScript初学者 - 按价值选择下拉选项可能会对您有所帮助.
<a href="javascript:void(0);" onclick="selectItemByValue(document.getElementById('personlist'),11)">change</a>
function selectItemByValue(elmnt, value){
for(var i=0; i < elmnt.options.length; i++)
{
if(elmnt.options[i].value === value) {
elmnt.selectedIndex = i;
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
cas*_*nos 17
您还可以像这样更改select.options.selectedIndex DOM属性:
function selectOption(index){
document.getElementById("select_id").options.selectedIndex = index;
}
Run Code Online (Sandbox Code Playgroud)
<p>
<select id="select_id">
<option selected>first option</option>
<option>second option</option>
<option>third option</option>
</select>
</p>
<p>
<button onclick="selectOption(0);">Select first option</button>
<button onclick="selectOption(1);">Select second option</button>
<button onclick="selectOption(2);">Select third option</button>
</p>
Run Code Online (Sandbox Code Playgroud)
小智 6
如果您使用 javascript 添加选项
function AddNewOption(userRoutes, text, id)
{
var option = document.createElement("option");
option.text = text;
option.value = id;
option.selected = "selected";
userdRoutes.add(option);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您也可以使用 JQuery
$(document).ready(function () {
$('#personlist').val("10");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
412818 次 |
最近记录: |