Home  >  Article  >  Web Front-end  >  Example of js operation that encapsulates the select tag of html_javascript skills

Example of js operation that encapsulates the select tag of html_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:30:411203browse
Copy code The code is as follows:

function BindSelect(id,dataList,fieldtext,fieldValue) {
//Bind a certain data source, fieldtext is the text field that needs to be bound, fieldValue is the value field that needs to be bound
var select = $("#" id)[0];
for (var i = 0; i < dataList.length; i ) {
select.options.add(new Option(eval("dataList[" i "]." fieldtext), eval("dataList[" i "]." fieldValue)));
}
}
function BindSelectOptions(id, OptionList) {
var select = $("#" id)[0];
for (var i = 0 ; i < OptionList.length; i ) {
select.options.add(new Option(OptionList[i].Text, OptionList[i].Value));
}
}
function ClearAllItems(id) {//Clear all options
var select = $("#" id)[0];
select.options.length = 0;
}
function AddOneItem( id, text, value) {//Add an option
var select = $("#" id)[0];
select.options.add(new Option(text, value));
}
function selectOneOption(id, selectValue) {//Select an option based on the value
var select = $("#" id)[0];
var len = select.options.length;
for (var i = 0; i < len; i ) {
if (select.options[i].value == selectValue) {
select.options[i].selected = true;
break;
}
}
}
function selectOneOptionByIndex(id, index) {/// /According to the subscript, select an option

var select = $("#" id)[0];
var len = select.options.length;
if ( index >= 0 && index <= len) {
select.options[index].selected = true;
}
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn