minimum
홈
태그
방명록
programing
을 통해 반복합니다.을 통해 반복합니다.
minimums
2023. 7. 27. 21:47
반응형
을 통해 반복합니다.
options 옵션들I have a <select> element in HTML. This element represents a drop down list.HTML에 <선택> 요소가 있습니다. 이 요소는 드롭다운 목록을 나타냅니다. I'm trying to understand how to iterate through the options in the <select> element via JQuery.저는 JQuery를 통해 <select> 요소의 옵션을 통해 반복하는 방법을 이해하려고 합니다. How do I use JQuery to display the value and text of each option in a <select> element?JQuery를 사용하여 <select> 요소에서 각 옵션의 값과 텍스트를 표시하는 방법은 무엇입니까? I just want to display them in an alert() box.$("#selectId > option").each(function() { 경보() 상자에 표시하고 싶습니다. $("#selectId > 옵션"). 각(function() { alert(this.text + ' ' + this.value);alert(이 .text + ' + this.value); }); }); http://api.jquery.com/each/ http://api.jquery.com/each/ http://jsfiddle.net/Rx3AP/ http://jsfiddle.net/Rx3AP/This worked for me 이것은 나에게 효과가 있었습니다.$(function() { $(함수: {} $("#select option").each(function(i){ $("#선택 옵션"). 각(함수(i){ alert($(this).text() + " : " + $(this).val()); alert(this).text() + " :" + $(this).val(); }); });}); });can also Use parameterized each with index and the element.또한 매개 변수화된 각 항목을 인덱스 및 요소와 함께 사용할 수 있습니다. $('#select$('#선택IntegrationConf').find('option').each(function(index,element){ IntegrationConf').find('option').각 함수(인덱스, 요소){ console.log(index); console.log(인덱스); console.log(element.value); console.log(session.value); console.log(element.text); console.log(console.text); }); }); // this will also work 이것도 효과가 있을 것입니다.$('#select$('#선택IntegrationConf option').each(function(index,element){ IntegrationConf 옵션').각 함수(인덱스, 요소){ console.log(index); console.log(인덱스); console.log(element.value); console.log(session.value); console.log(element.text); console.log(console.text); }); });And the requisite, non-jquery way, for followers, since google seems to send everyone here: 그리고 구글이 모든 사람들을 여기로 보내는 것처럼 보이기 때문에 팔로워들에게 필요한 비재쿼리적인 방법은 다음과 같습니다. var select = document.getElementById("select_id"); var select = document.getElementById("select_id"); for (var i = 0; i < select.length; i++){ (vari = 0; i < select.length; i++){ var option = select.var 옵션 = 선택합니다.options[i]; 옵션[i]; // now have option.이제 선택권이 있습니다.text, option.value 텍스트, 옵션.값 } }If you don't want Jquery (and can use ES6) 만약 당신이 Jquery를 원하지 않는다면 (그리고 ES6를 사용할 수 있습니다.for (const option of document.getElementById('mySelect')) { for(document.getElementById('mySelect')의 const 옵션) { console.log(option);console.log(옵션); } }You can try like this too.여러분도 이렇게 해보세요. Your HTML Code HTML 코드<select id="mySelectionBox"> <select id="mySelectionBox"> <option value="hello"<옵션 값="안녕하세요">Foo</option> >푸</옵션> <option value="hello1"<옵션 값="안녕하세요1">Foo1</option> >Foo1</옵션> <option value="hello2"<옵션 값="안녕하세요2">Foo2</option> >Foo2</옵션> <option value="hello3"<옵션값="안녕하세요3">Foo3</option> >Foo3</옵션></select> </select> You JQuery Code 유 J쿼리 코드$("#mySelectionBox option").each(function() { $("#mySelectionBox 옵션"). 각(function() { alert(this.text + ' ' + this.value);alert(이 .text + ' + this.value); }); }); OR ORvar select = $('#mySelectionBox')[0]; var select = $('#mySelectBox')[0]; for (var i = 0; i < select.length; i++){ (vari = 0; i < select.length; i++){ var option = select.options[i]; var 옵션 = select.vari[i]; alert (option.alert(옵션).text + ' ' + option.value); 텍스트 + ' + 옵션.value); } }$.each($("#MySelect option"), function(){ $.각($("#MySelect 옵션"), 함수(){ alert($(this).text() + " - " + $(this).val()); alert(this).text() + " - " + $(this).val(); }); });Another variation on the already proposed answers without jQuery.jQuery가 없는 이미 제안된 답변의 다른 변형입니다. Object.values(document.getElementById('mySelect').options).Object.values(document.getElementById('mySelect').옵션).forEach(option => alert(option))각(옵션 = > alert(옵션)) After try several code, and still not working, I go to official documentation of select2.js.몇 개의 코드를 시도한 후에도 여전히 작동하지 않는 select2.js의 공식 문서로 이동합니다. here the link: https://select2.org/programmatic-control/add-select-clear-items 여기 링크: https://select2.org/programmatic-control/add-select-clear-itemsfrom that the way to clear selection select2 js is: 선택 항목 2 js를 삭제하는 방법은 다음과 같습니다.$('#mySelect2').val(null).$('#mySelect2'), val(null)입니다.trigger('change');트리거 "변경"; Reference언급URL : https://stackoverflow.com/questions/2950152/iterate-through-select-optionsURL : https://stackoverflow.com/questions/2950152/iterate-through-select-options
반응형
공유하기
게시글 관리
minimum
'
programing
' 카테고리의 다른 글
엔티티 유형 MVC5 EF6의 사용자
(0)
2023.08.01
코드의 일부를 비활성화하는 간단한 방법
(0)
2023.07.27
ios 오류와 함께 코르도바 실행..명령에 대한 오류 코드 65: 인수를 사용한 xcodebuild:
(0)
2023.07.27
UIPageViewController: 현재 표시되는 보기를 반환
(0)
2023.07.27
SQL 화학에서 MariaDB의 COLUMN_GET() 사용
(0)
2023.07.27
티스토리툴바