nicesource
홈
태그
방명록
source
를 「Atribute」에 할 수 ?를 「Atribute」에 할 수 ?를 「Atribute」에 할 수 ?
nicesource
2022. 12. 4. 22:31
반응형
를 「Atribute」에 할 수 ?
fields in HTML5? HTML5의 필드?How can I check if a user has selected something from a <select> field in HTML5? 사용자가 HTML5의 <select> 필드에서 어떤 것을 선택했는지 확인하려면 어떻게 해야 합니까?I see <select> doesn't support the new required attribute...<select>는 새로운 필수 속성을 지원하지 않는 것을 알 수 있습니다. do I have to use JavaScript then?그럼 자바스크립트를 사용해야 하나요? Or is there something I’m missing?아니면 제가 놓친 게 있나요? :/Mandatory: Have the first value empty - required works on empty values :/필수: 첫 번째 값은 비워 둡니다.필요한 값은 빈 값으로 동작합니다.Prerequisites: correct html5 DOCTYPE and a named input field 필수 구성 요소: 올바른 html5 DO3}PE 및 명명된 입력 필드<select name="somename" required> <선택명="소면명"필수><option value="">Please select</option> <option value="> 선택해주세요</option><option value="one"<옵션값="1">One</option> 1개 </옵션></select> </select> As per the documentation (the listing and bold is mine) 설명서에 따라 (목록과 굵은 글씨는 제 것입니다) The required attribute is a boolean attribute.필수 Atribut은 부울 Atribute입니다. When specified, the user will be required to select a value before submitting the form.지정 시 사용자는 폼을 전송하기 전에 값을 선택해야 합니다. If a select element 선택한 요소가 has a required attribute specified, 에는 필수 속성이 지정되어 있습니다. does not have a multiple attribute specified, 에는 복수의 Atribute가 지정되어 있지 않습니다. and has a display size of 1 (do not have SIZE=2 or more - omit it if not needed); 표시 크기가 1(SIZE=2 이상일 수 없습니다. 필요하지 않은 경우 생략합니다.); and if the value of the first option element in the select element's list of options (if any) is the empty string (i.e. present as value=""), 그리고 선택 요소의 옵션 목록(있는 경우)에 있는 첫 번째 옵션 요소의 값이 빈 문자열(예: value="로 표시됨)인 경우, and that option element's parent node is the select element (and not an optgroup element), 옵션 요소의 부모 노드는 (옵트 그룹 요소가 아닌) 선택 요소입니다. then that option is the select element's placeholder label option.이 옵션은 선택 요소의 자리 표시자 레이블 옵션입니다. The <select> element does support the required attribute, as per the spec: <select> 요소는 사양에 따라 필요한 속성을 지원합니다. http://dev.w3.org/html5/spec-author-view/the-select-element.html#the-select-element http://dev.w3.org/html5/spec-author-view/the-select-element.html#the-select-element Which browser doesn’t honour this?어떤 브라우저가 이 기능을 지원하지 않나요? (Of course, you have to validate on the server anyway, as you can’t guarantee that users will have JavaScript enabled.)(물론 사용자가 JavaScript를 사용하도록 설정할 수 없기 때문에 서버에서 검증해야 합니다.)Yes, it's working: 네, 효과가 있습니다.<select name="somename" required> <선택명="소면명"필수> <option value="">Please select</option> <option value="> 선택해주세요</option> <option value="one"<옵션값="1">One</option> 1개 </옵션></select> </select> you have to keep first option blank.첫 번째 옵션은 공백으로 두어야 합니다.You can use the selected attribute for the option element to select a choice by default.옵션 요소에 대해 선택한 속성을 사용하여 기본적으로 선택할 수 있습니다. You can use the required attribute for the select element to ensure that the user selects something.선택 요소에 필요한 속성을 사용하여 사용자가 항목을 선택하도록 할 수 있습니다. In Javascript, you can check the selectedJavascript에서 선택한 항목을 확인할 수 있습니다.Index property to get the index of the selected option, or you can check the value property to get the value of the selected option.인덱스 속성을 선택하여 선택한 옵션의 인덱스를 가져오거나 값 속성을 선택하여 선택한 옵션의 값을 가져올 수 있습니다. According to the HTML5 spec, selected선택한 HTML5 사양에 따라Index "returns the index of the first selected item, if any, or −1 if there is no selected item.색인 "첫 번째 선택한 항목의 색인(있는 경우)을 반환합니다. 선택한 항목이 없으면 -1을 반환합니다. And value "returns the value of the first selected item, if any, or the empty string if there is no selected item."value는 "첫 번째 선택한 항목의 값이 있으면 반환하고, 선택한 항목이 없으면 빈 문자열"을 반환합니다. So if selected그래서 선택되면Index = -1, then you know they haven't selected anything.지수 = -1이면 아무것도 선택하지 않았다는 것을 알 수 있습니다. <button type="button" onclick="displaySelection()"><버튼 타입="버튼" on click="display Selection()">What did I pick?내가 뭘 골랐지?</button> </버튼><script> <스크립트> function displaySelection() display Selection() 함수 { { var mySelect = document.getElementById("someSelectElement");var mySelect = document.getElementById("someSelectElement"); var mySelection = mySelect.var mySelection = mySelect.selected선택된Index; 색인 alert(mySelection);alert(my Selection); } }</script> </script>You need to set the value attribute of option to the empty string: 옵션의 값 속성을 빈 문자열로 설정해야 합니다.<select name="status" required> < select name = " status " required > <option selected disabled value="">what's your status?<option selected disabled value="> 상태는?</option> </옵션> <option value="code">coding</option> < option value = "code" > 명령어 </option> <option value="sleep">sleeping</option> <option value="sleeve"> 명령어 </option></select> </select> select will return the value of the selected option to the server when the user presses submit on the form.사용자가 폼에서 [Submit]를 누르면 선택한 옵션의 값이 서버로 반환됩니다. An empty value is the same as an empty text input -> raising the required message.빈 값은 빈 텍스트 입력 -> 필요한 메시지를 표시하는 것과 같습니다. w3schools w3의 The value attribute specifies the value to be sent to a server when a form is submitted.value 속성은 폼이 전송될 때 서버로 전송되는 값을 지정합니다. Example 예<form action=""> <폼 액션="> <select required> <선택 필수> <option selected disabled value="">choose</option> <option selected disabled value="> 선택 </option> <option value="red">red</option> <option value="red">빨간색 </option> <option value="yellow">yellow</option> <option value="yellow"> 노란색 </option> <option value="green">green</option> <option value="green"> 녹색 </option> <option value="grey">grey</option> <option value="grey">그레이 </option> </select> </select><input type="submit"<입력 타입="접속"> ></form> </폼>try this, this gonna work, I have tried this and this works.이거 해 봐, 이거 할 수 있을 거야, 나도 이거 해 봤고 또 이거 할 수 있어. <!DOCTYPE html> <!DOSCTYPE html><html> <blocks><body> <본문> <form action="#"> <폼 액션="#"><select required> <선택 필수> <option value="">None</option> <option value="> 없음 </option> <option value="volvo">Volvo</option> <option value="volvo">볼보 </option> <option value="saab">Saab</option> <option value="saab"> Saab </option> <option value="mercedes"<option value = "선택사항">Mercedes</option> 인정 </옵션> <option value="audi"><option value="filename">Audi</option> Audi </옵션></select> </select><input type="submit"<입력 타입="접속"> ></form> </폼> </body> </body></html> </filters>Make the value of first item of selection box to blank.선택 상자의 첫 번째 항목 값을 공백으로 만듭니다. So when every you post the FORM you get blank value and using this way you would know that user hasn't selected anything from dropdown.따라서 폼을 투고할 때마다 공백 값이 표시되며, 이 방법을 사용하면 사용자가 드롭다운에서 아무것도 선택하지 않았음을 알 수 있습니다. <select name="user_role" required> < select name = "user_role" 필수> <option value="">-Select-</option> <option value=">-선택-/option> <option value="User">User</option> <option value="User"> 사용자 </option> <option value="Admin"<option value="Admin">Admin</option> > 관리 </옵션></select> </select>first you have to assign blank value in first option. i.e. Select here.than only required will work.먼저 첫 번째 옵션에서 빈 값을 할당해야 합니다. 즉, 여기를 선택합니다.필요한 경우만 사용할 수 있습니다.Works perfectly fine if the first option's value is null.첫 번째 옵션의 값이 null일 경우 완벽하게 작동합니다. Explanation : The HTML5 will read a null value on button submit.설명 : HTML5는 버튼을 전송하면 늘 값을 읽습니다. If not null (value attribute), the selected value is assumed not to be null hence the validation would have worked i.e by checking if there's been data in the option tag.null(값 속성)이 아닌 경우 선택한 값은 null이 아닌 것으로 간주되므로 옵션 태그에 데이터가 있는지 확인하는 방법으로 검증이 수행되었을 수 있습니다. Therefore it will not produce the validation method.따라서 검증 방법은 생성되지 않습니다. However, i guess the other side becomes clear, if the value attribute is set to null ie (value = "" ), HTML5 will detect an empty value on the first or rather the default selected option thus giving out the validation message.그러나 상대방이 명확해지면 값 속성이 null ie(값 = ")로 설정되어 있으면 HTML5는 처음 선택한 옵션에서 빈 값을 감지하여 검증 메시지를 제공합니다. Thanks for asking.물어봐줘서 고마워. Happy to help.내가 기꺼이 도와줄게. Glad to know if i did.그랬다면 다행이군In html5 you can do using the full expression: html5에서는 완전한 식을 사용할 수 있습니다.<select required="required"> <required="required" 선택> I don't know why the short expression doesn't work, but try this one.왜 짧은 표현이 통하지 않는지는 모르겠지만, 이것을 사용해 보세요. It will solve.해결될 거야.Try this 이거 드셔보세요<select> <선택><option value="" style="display:none"><option value=" style="display: none">Please select</option> 선택해주세요</option><option value="one"<옵션값="1">One</option> 1개 </옵션></select> </select>You can do it also dynamically with JQuery JQuery를 사용하여 동적으로 수행할 수도 있습니다.Set required 필수 설정$("#select1").attr('required', 'required');$("#select1.".attr "required", "required"); Remove required 삭제 필요$("#select1").removeAttr('required');$("#select1").removeAttr('required'); Reference언급URL : https://stackoverflow.com/questions/6048710/can-i-apply-the-required-attribute-to-select-fields-in-html5URL : https://stackoverflow.com/questions/6048710/can-i-apply-the-required-attribute-to-select-fields-in-html5
반응형
공유하기
게시글 관리
nicesource
'
source
' 카테고리의 다른 글
rewriteBatchedStatements=true를 사용하는 MySQL 및 JDBC
(0)
2022.12.04
Node.js에서 로컬 IP 주소를 가져옵니다.
(0)
2022.12.04
외부 js 파일에서 저장소로 액세스(nuxtjs)
(0)
2022.12.04
@IdClass 또는 @EmbeddedId 중 어떤 주석을 사용해야 합니까?
(0)
2022.12.04
Vue JS를 사용하여 HTML에서 동일한 태그에 "v-if"와 "v-else"를 사용하는 방법은 무엇입니까?
(0)
2022.12.04
티스토리툴바