さまざまな html 入力フィールドを持つフォームがあります...
1) <input type="text">
2) <textarea></textarea>
3) <input type="checkbox">
4) <input type="radio">
5) <select></select>
jQuery を使用して入力フィールドのタイプを特定するにはどうすればよいでしょうか。例: input = "select" かどうかを確認したい場合は、何かを実行します。
さまざまな html 入力フィールドを持つフォームがあります...
1) <input type="text">
2) <textarea></textarea>
3) <input type="checkbox">
4) <input type="radio">
5) <select></select>
jQuery を使用して入力フィールドのタイプを特定するにはどうすればよいでしょうか。例: input = "select" かどうかを確認したい場合は、何かを実行します。
$('input') // selects all types of inputs
$('input:checkbox') // selects checkboxes
$('select') // selects select element
$('input:radio') // selects radio inputs
$('input[type="text"]') // selects text inputs
を使用できます。これは、どのタイプの、またはがイベントのターゲットであるかevent.target.type
を警告します。input
textrea
select
$('input, textarea, select').change(function(event){
alert(event.target.type)
})
これを行うには、これらの各要素を引き出すセレクターを作成してから、それらを繰り返し処理してタイプを確認します。このようなもの:
$('input, textarea, select').each(function() {
var el = $(this);
if(el.is('input')) { //we are dealing with an input
var type = el.attr('type'); //will either be 'text', 'radio', or 'checkbox
} else if(el.is('select')) { //we are dealing with a select
//code here
} else { //we are dealing with a textarea
//code here
}
});
1、3、4 の場合:
$("input").attr('type');
JQuery is 構文を使用することをお勧めします
の線に沿った何か
$(document).ready(function() {
var items = $("input");
if(items.first().is("input[type=text]")) {
alert("Text type");
}
});
ここで確認できます http://jsfiddle.net/JRLn9/2/
var tipo = $('#elemento14').attr('type');