jquery를 사용하여 입력 유형을 얻는 방법은 무엇입니까?
입력 유형이 항상 다른 페이지가 있으며 입력 유형에 따라 값을 가져와야합니다. 따라서 유형이 라디오 인 경우 어느 것이 선택되어 있는지 확인해야합니다. 확인란이면 이제 어느 것이 선택되어 있는지 확인해야합니다. 드롭 다운 인 경우 어느 것이 선택되어 있는지 알고 있어야합니다. 텍스트 / 텍스트 영역 값을 알아야합니다.
그 방법에 대한 아이디어가 있습니까?
이 답변의 인기와 속성 및 속성에 관한 버전 1.9 및 2.0의 jQuery 변경으로 인해 입력에 대한 속성 / 속성에 액세스 할 때 작동 방식을 확인하기 위해 메모와 바이올린을 추가했습니다. 버튼과 일부 선택. 여기에 바이올린 : http://jsfiddle.net/pVBU8/1/
모든 입력을 얻으십시오 :
var allInputs = $(":input");
모든 입력 유형을 얻으십시오.
allInputs.attr('type');
값을 얻으십시오 :
allInputs.val();
참고 : .val ()은 관련이있는 유형에 대해 : checked와 동일하지 않습니다. 사용하다:
.attr("checked");
수정 : 2013 년 2 월 1 일-re : jQuery 1.9 attr ()이 아닌 prop ()을 사용하십시오 .attr은 변경된 속성에 적절한 값을 반환하지 않기 때문입니다.
.prop('checked');
또는 단순히
$(this).checked;
수표의 가치를 얻으려면-현재 수표가 무엇이든간에. 또는``: checked ''를 사용하여 확인 된 항목 만 원하는 경우에만 사용하십시오.
편집 : 다음은 유형을 얻는 또 다른 방법입니다.
var allCheckboxes=$('[type=checkbox]');
EDIT2 : 다음의 형식에 유의하십시오.
$('input:radio');
이상하다
$(':radio');
둘 다와 동일합니다.
$('input[type=radio]');
그러나 "입력"이 바람직하므로 입력 만 가져오고 다음과 같은 형식을 $(':radio')
사용할 때 범용 '*'를 사용하지 않습니다.$('*:radio');
편집 2015 년 8 월 19 일 : $('input[type=radio]');
최신 브라우저가 라디오 입력 검색을 최적화 할 수 있도록 선호 사항을 사용해야합니다.
의견 수정 2013 년 2 월 1 일 다시 : 요소 @dariomac 선택
$('select').prop("type");
"multiple"속성에 따라 "select-one"또는 "select-multiple"을 반환합니다.
$('select')[0].type
첫 번째 선택 (있는 경우)에 대해 동일을 반환합니다. 과
($('select')[0]?$('select')[0].type:"howdy")
존재하는 경우 유형을 반환하거나 존재하지 않는 경우 "howdy"를 반환합니다.
$('select').prop('type');
DOM에있는 첫 번째 속성이 있으면 속성을 반환하고, 존재하지 않으면 "정의되지 않은"속성을 반환합니다.
$('select').type
존재하는 경우 첫 번째 유형이 있거나 존재하지 않는 경우 오류를 리턴합니다.
다음을 수행 할 수 있습니다.
var inputType = $('#inputid').attr('type');
당신이 말하고있는 것이 입력 유형에 대해 걱정하지 않고 값을 가진 양식 내부의 모든 입력을 얻으려는 경우 다음을 시도하십시오.
예 : http://jsfiddle.net/nfLfa/
var $inputs = $('form').find(':checked,:selected,:text,textarea').filter(function() {
return $.trim( this.value ) != '';
});
이제 값이있는 입력 요소 세트가 있어야합니다.
배열에 값을 넣을 수 있습니다.
var array = $inputs.map(function(){
return this.value;
}).get();
또는 직렬화 할 수 있습니다.
var serialized = $inputs.serialize();
GetValue = function(id) {
var tag = this.tagName;
var type = this.attr("type");
if (tag == "input" && (type == "checkbox" || type == "radio"))
return this.is(":checked");
return this.val();
};
$("#yourobj").attr('type');
보고 시작하기 가장 좋은 곳은 http://api.jquery.com/category/selectors/
This will give you a good set of examples.
Ultamatly the selecting of elements in the DOM is achived using CSS selectors so if you think about getting an element by id you will want to use $('#elementId'), if you want all the input tags use $('input') and finaly the part i think you'll want if you want all input tags with a type of checkbox use $('input, [type=checkbox])
Note: You'll find most of the values you want are on attributes so the css selector for attributes is: [attributeName=value]
Just because you asked for the dropdown as aposed to a listbox try the following:
$('select, [size]).each(function(){
var selectedItem = $('select, [select]', this).first();
});
The code was from memory so please accound for small errors
var val = $('input:checkbox:checked, input:radio:checked, \
select option:selected, textarea, input:text',
$('#container')).val();
Comments:
I assume, that there is exactly one form element, that can be either a textarea, input field, select form, a set of radio buttons or a single checkbox (you will have to update my code if you need more checkboxes).
The element in question lives inside an element with ID
container
(to remove ambiguences with other elements on the page).The code will then return the value of the first matching element it finds. Since I use
:checked
and so on, this should always be exactly the value of what you're looking for.
It would seem that the attr functionality is getting further deprecated
$(this).attr('TYPE')
undefined
$(this).prop('type')
"text"
In my application I ended up with two different elements having the same id (bad). One element was a div and the other an input. I was trying to sum up my inputs and took me a while to realise the duplicate ids. By placing the type of the element I wanted in front of #, I was able to grab the value of the input element and discard the div:
$("input#_myelementid").val();
I hope it helps.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".show-pwd").click(function(){
alert();
var x = $("#myInput");
if (x[0].type === "password") {
x[0].type = "text";
} else {
x[0].type = "password";
}
});
});
</script>
</head>
<body>
<p>Click the radio button to toggle between password visibility:</p>Password:
<input type="password" value="FakePSW" id="myInput">
<br>
<br>
<input type="checkbox" class="show-pwd">Show Password</body>
</html>
참고URL : https://stackoverflow.com/questions/3165413/how-to-get-input-type-using-jquery
'Programing' 카테고리의 다른 글
Mockito는 모든 클래스 인수와 일치 (0) | 2020.06.26 |
---|---|
열거 형의 최대 값 얻기 (0) | 2020.06.26 |
프로비저닝 프로파일을 생성하지 못했습니다. (0) | 2020.06.25 |
BIST HISTSIZE 대 HISTFILESIZE? (0) | 2020.06.25 |
도커 컨테이너에서 ps 명령이 작동하지 않습니다 (0) | 2020.06.25 |