2011-10-24 5 views
0

저는 대화식 양식을 작성하고 사용자의 대답에 따라 필드를로드하고 있습니다. 지금은 두 종류의 객체 "입력 라디오"및 "텍스트 영역"이 있습니다. JQuery를 통해 답변의 가치를 얻었으므로 맨 마지막에로드 된 필드 세트 내에서 어떤 객체를 처리하는지 어떻게 알 수 있습니까?JQuery : 우리가 다루고있는 객체의 종류를 아는 법?

$.fn.getNodeName = function() { // returns the nodeName of the first matched element, or "" 
    return this[0] ? this[0].nodeName : ""; 
}; 

//var $something = $('input[type=radio]:checked:last'); 
var $something = $('fieldset:last').parent('nth-child(2)'); 

//$('#news ul li:first-child').addClass('active'); 

alert($something.getNodeName()); 

을하지만 ... 감사를 작동하지 않습니다 :

이 내가 노력하고 있습니다 것입니다.

+0

"radio'을':"유형을 지정하는 또 다른 방법은 같은 반환 값에 추가 속성을 추가하는 것입니다 . 아마도 대신 해당 부분에 대한 속성 선택기를 사용하려고했을 것입니다. 'input [type = radio] : checked : last'? –

+0

업데이트 된 질문 @Jordan – Roger

답변

2

I의 요소는 입력 요소 때의 jQuery 함수 또한 type를 반환하도록 확장 가지고 가능한 반환 값

$.fn.getNodeName = function() { 
    var elem = this[0]; //Get the first element of the jQuery object 
    if(!elem) return ""; 
    var name = elem.nodeName.toLowerCase(); 
    return name == "input" ? "input[type=" + elem.type + "]" : name; 
}; 

예 :
textarea, input[type=text], input[type=button] ...

다른 출력 형식을 선호하는 경우 기능을 조정하십시오. CSS의 기존 pseudoselector되지 않습니다 :

$.fn.getNodeName = function() { 
    var elem = this[0]; //Get the first element of the jQuery object 
    if (!elem) return ""; 
    var name = elem.nodeName.toLowerCase(); 
    if (name == "input") { 
     name.type = elem.type; 
    } 
    return name; 
}; 
+0

답변입니다! 고맙습니다. – Roger

관련 문제