input
要素がbutton
タイプであるtext
か、jqueryのタイプであるかをどのように判断できますか?
ありがとう
あなたが持っている場合:
var field = $("some selector that finds the input");
それで
field[0].tagName
...INPUT
またはBUTTON
またはSELECT
またはTEXTAREA
のいずれかになり、フィールドの作成に使用されたタグを示します。
の場合INPUT
、
field[0].type
// or
field.attr('type')
// or
field.prop('type')
...input
タグのタイプになります (例: "text"
、"button"
など)。
HTML:
<input type="text" value="text field">
<br><input type="button" value="input button">
<br><select><option>select</option></select>
<br><textarea>textarea</textarea>
<br><button>button</button>
JavaScript:
jQuery(function($) {
$("input, select, textarea, button").each(function(index) {
var msg;
var field = $(this);
msg = "[" + index + "]: tagName is " + field[0].tagName;
if (field[0].tagName === "INPUT") {
msg += ", and type is " + field.prop('type');
}
display(msg);
});
function display(msg) {
$("<p>").html(msg).appendTo(document.body);
}
});
出力:
[0]: tagName は INPUT、type はテキスト [1]: tagName は INPUT、type はボタン [2]: tagName は SELECT です [3]: tagName は TEXTAREA です [4]: tagName は BUTTON です
field = $(this)
ここでは、各要素を jQuery ラッパーで囲むためだけに使用したことに注意してください。生の DOM 要素参照が既にある場合 (そのループで行うように、 as this
)、それを行う必要はなく、elm.tagName
and をelm.type
直接使用するだけです。
要素のタグ名を確認します。
element.prop('tagName')
<input>
タグについては、タイプも確認してください。
element.prop('type');