HTML 선택 옵션 상세 설명
이 글에서는 HTML 선택 옵션 상세 설명을 소개합니다
javascriptHTML(선택 옵션) 상세 설명
기본 이해:
var e = document.getElementById("selectId");
e.options= new Option("text","value");
//옵션 객체를 생성합니다. 즉,
mySelect.id = "mySelect";
document.body.appendChild(mySelect);
}
obj.add(new Option("text","value")) //이것은 IE에서만 유효합니다
obj.options.add( new Option("text"," value")); //IE 및 Firefox와 호환됩니다
3 모든 옵션 삭제 옵션
함수 제거All(){
var obj=document.getElementById('mySelect');
obj.options.length=0;
}
4. 옵션 삭제 옵션
function RemoveOne(){
var obj=document.getElementById('mySelect');
//index, 삭제할 옵션의 일련번호 삭제되었습니다. 현재 선택한 옵션의 일련번호
var index=obj.selectedIndex;
obj.options.remove(index)
}
5. 옵션 값
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //일련번호, 현재 선택한 옵션의 일련번호 가져오기
var val = obj.options [index].value;
6. 옵션 옵션의 텍스트 가져오기
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //일련번호, 현재 선택한 옵션의 일련번호 가져오기
var val = obj.options[index].text;
7. 옵션 옵션 수정
var obj=document.getElementById('mySelect');
var index=obj.selectedIndex; //일련번호, 현재 선택한 옵션의 일련번호 가져오기
var val = obj.options[index]=new Option(" New text", "new value");
8. 삭제 선택
함수 RemoveSelect() {
var mySelect = document.getElementById("mySelect");
mySelect .parentNode.removeChild(mySelect);
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//ZH-CN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html"> <head> <script language=JavaScript> function $(id) { return document.getElementById(id) } function show() { var selectObj=$("area") var myOption=document.createElement("option") myOption.setAttribute("value","10") myOption.appendChild(document.createTextNode("上海")) var myOption1=document.createElement("option") myOption1.setAttribute("value","100") myOption1.appendChild(document.createTextNode("南京")) selectObj.appendChild(myOption) selectObj.appendChild(myOption1) } function choice() { var index=$("area").selectedIndex; var val=$("area").options[index].getAttribute("value") if(val==10) { var i=$("context").childNodes.length-1; var remobj=$("context").childNodes[i]; remobj.removeNode(true) var sh=document.createElement("select") sh.add(new Option("浦东新区","101")) sh.add(new Option("黄浦区","102")) sh.add(new Option("徐汇区","103")) sh.add(new Option("普陀区","104")) $("context").appendChild(sh) } if(val==100) { var i=$("context").childNodes.length-1; var remobj=$("context").childNodes[i]; remobj.removeNode(true) var nj=document.createElement("select") nj.add(new Option("玄武区","201")) nj.add(new Option("白下区","202")) nj.add(new Option("下关区","203")) nj.add(new Option("栖霞区","204")) $("context").appendChild(nj) } } function calc() { var x=$("context").childNodes.length-1; alert(x) } function remove() { var i=$("context").childNodes.length-1; var remobj=$("context").childNodes[i]; remobj.removeNode(true) } </script> <body> <p id="context"> <select id="area" on change="choice()"> </select> </p> <input type=button value="显示" onclick="show()"> <input type=button value="计算结点" onclick="calc()"> <input type=button value="删除" onclick="remove()"> </body> </html>
이런 점을 토대로 JQEURY를 사용했습니다. AJAX+JSON 다음과 같은 작은 기능을 구현합니다.
JS 코드: (SELECT 관련 코드만 가져왔습니다)
/** * @description 构件联动下拉列表 (用JQUERY 的AJAX配合JSON实现) * @prarm selectId 下拉列表的ID * @prarm method 要调用的方法名称 * @prarm temp 此处存放软件ID * @prarm url 要跳转的地址 */ function linkAgeJson(selectId,method,temp,url){ $j.ajax({ type: "get",//使用get方法访问后台 dataType: "json",//返回json格式的数据 url: url,//要访问的后台地址 data: "method=" + method+"&temp="+temp,//要发送的数据 success: function(msg){//msg为返回的数据,在这里做数据绑定 var data = msg.lists; coverJsonToHtml(selectId,data); } }); } /** * @description 将JSON数据转换成HTML数据格式 * @prarm selectId 下拉列表的ID * @prarm nodeArray 返回的JSON数组 * */ function coverJsonToHtml(selectId,nodeArray){ //get select var tempSelect=$j("#"+selectId); //clear select value isClearSelect(selectId,'0'); var tempOption=null; for(var i=0;i<nodeArray.length;i++){ //create select Option tempOption= $j('<option value="'+nodeArray[i].dm+'">'+nodeArray[i].mc+'</option> '); //put Option to select tempSelect.append(tempOption); } // 获取退化构件列表 getCpgjThgl(selectId,'thgjDm'); } /** * @description 清空下拉列表的值 * @prarm selectId 下拉列表的ID * @prarm index 开始清空的下标位置 */ function isClearSelect(selectId,index){ var length=document.getElementById(selectId).options.length; while(length!=index){ //长度是在变化的,因为必须重新获取 length=document.getElementById(selectId).options.length; for(var i=index;i<length;i++) document.getElementById(selectId).options.remove(i); length=length/2; } } /** * @description 获取退化构件列表 * @prarm selectId1 引用软件下拉列表的ID * @prarm selectId2 退化构件下拉列表的ID */ function getCpgjThgl(selectId1,selectId2){ var obj1=document.getElementById(selectId1);//引用软件下拉列表 var obj2=document.getElementById(selectId2);//退化构件下拉列表 var len=obj1.options.length; //当引用软件列表长度等于1时返回,不做操作 if(len==1){ return false; } //清空下拉列表的值,两种方式都可以 // isClearSelect(selectId2,'1'); document.getElementById(selectId2).length=1; for(var i=0;i<len; i++){ var option= obj1.options[i]; //引用软件被选中项不加入 if(i!=obj1.selectedIndex){ //克隆OPTION并添加到SELECT中 obj2.appendChild(option.cloneNode(true)); } } }
HTML 코드:
<TABLE width="100%" border=0 align="left" cellPadding=0 cellSpacing=1> <tr> <td class="Search_item_18"> <span class="Edit_mustinput">*</span>引用软件:</td> <td class="Search_content_82"> <input name="yyrjMc" id="yyrjMc" type="text" class="Search_input" tabindex="3" size="30" > <input name="yyrjDm" id="yyrjDm" type="hidden" > <input type="button" class="Search_button_select" onClick="linkAgeTree('linkage','yyrjtree','yyrjMc','yyrjDm','linkageTree','1');" value="选择..."> </td> </tr> <tr> <td class="Search_item"> <span class="Edit_mustinput">*</span>引用分版:</td> <td class="Search_content" id="yyfb"> <select name="yyfbDm" style="width:160" id="yyfbDm" onChange="getCpgjThgl('yyfbDm','thgjDm')"> </select> </td> </tr> <tr> <td class="Search_item">退化构件:</td> <td class="Search_content" id="thgj"> <select name="thgjDm" style="width:160" id="thgjDm"> <option value="-1" selected>无</option> </select> </td> </tr> </TABLE>
위 내용은 HTML 선택 옵션 상세 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











이 기사는 HTML & lt; Progress & Gt에 대해 설명합니다. 요소, 그 목적, 스타일 및 & lt; meter & gt의 차이; 요소. 주요 초점은 & lt; progress & gt; 작업 완료 및 & lt; meter & gt; Stati의 경우

이 기사는 HTML & LT; Datalist & GT에 대해 논의합니다. 자동 완성 제안을 제공하고, 사용자 경험을 향상시키고, 오류를 줄임으로써 양식을 향상시키는 요소. 문자 수 : 159

기사는 HTML5 크로스 브라우저 호환성을 보장하기위한 모범 사례에 대해 논의하고 기능 감지, 점진적 향상 및 테스트 방법에 중점을 둡니다.

이 기사는 HTML & lt; meter & gt에 대해 설명합니다. 범위 내에 스칼라 또는 분수 값을 표시하는 데 사용되는 요소 및 웹 개발의 일반적인 응용 프로그램. & lt; meter & gt; & lt; Progress & Gt; 그리고 Ex

이 기사에서는 브라우저에서 직접 사용자 입력을 검증하기 위해 필요한, Pattern, Min, Max 및 Length 한계와 같은 HTML5 양식 검증 속성을 사용하는 것에 대해 설명합니다.

이 기사는 모바일 장치의 반응 형 웹 디자인에 필수적인 Viewport Meta Tag에 대해 설명합니다. 적절한 사용이 최적의 컨텐츠 스케일링 및 사용자 상호 작용을 보장하는 방법을 설명하는 반면, 오용은 설계 및 접근성 문제로 이어질 수 있습니다.

이 기사는 & lt; iframe & gt; 외부 컨텐츠를 웹 페이지, 공통 용도, 보안 위험 및 객체 태그 및 API와 같은 대안을 포함시키는 태그의 목적.

GiteEpages 정적 웹 사이트 배포 실패 : 404 오류 문제 해결 및 해결시 Gitee ...
