开发者

How to get type of input object [duplicate]

开发者_开发知识库This question already has answers here: Closed 10 years ago.

Possible Duplicate:

How do I determine an HTML input element type upon an event using jQuery?

I want to get type of input object but I alway got "undefined".

What is missing it this code?

var c = $("input[name=\'lastName\']");
c.val("test");
alert(c.type);

My form

<form action="formAction.jsp" method="post">
    FirstName <input type="text" name="firstName" id="firstName"></input><br/>
    LastName <input type="text" name="lastName"></input><br/>
    Address <input type="text" name="address"></input><br/>
    Tel <input type="text" name="tel"></input><br/>
    Email <input type="text" name="email"></input><br/>
</form>


$(c).attr('type')


jQuery does not return an ordinary element object, which has its attributes as properties. It returns a jQuery wrapper; you can access its attributes using attr(), so what you want is:

alert(c.attr('type'));


Change this:

 var c = $("input[name=\'lastName\']");

To:

 var c = $("input[name='lastName']");
 alert(c.type);

...

Alternatively:

 alert($(c).attr('type'));


since you're using jQuery, you can do this:

$( 'input[name="lastName"]' ).attr( 'type' );//returns text

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜