10

さまざまな html 入力フィールドを持つフォームがあります...

1) <input type="text">
2) <textarea></textarea> 
3) <input type="checkbox">
4) <input type="radio">
5) <select></select>

jQuery を使用して入力フィールドのタイプを特定するにはどうすればよいでしょうか。例: input = "select" かどうかを確認したい場合は、何かを実行します。

4

7 に答える 7

21
$('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を警告します。inputtextreaselect

$('input, textarea, select').change(function(event){
   alert(event.target.type)
})

http://jsfiddle.net/Q4BNH/

于 2012-06-26T20:45:28.027 に答える
15

jquery の .is() を使用できます。例えば

  if ( $(this).is("input") )   //or 
  if ( $(this).is('input:text') )

詳細はこちら

于 2012-06-26T20:46:54.953 に答える
1

これを行うには、これらの各要素を引き出すセレクターを作成してから、それらを繰り返し処理してタイプを確認します。このようなもの:

$('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
    }
});
于 2012-06-26T20:48:08.723 に答える
1

1、3、4 の場合:

$("input").attr('type');
于 2012-06-26T20:44:39.187 に答える
1

JQuery is 構文を使用することをお勧めします

http://api.jquery.com/is/

の線に沿った何か

$(document).ready(function() {
    var items = $("input");
    if(items.first().is("input[type=text]")) {
     alert("Text type");            
    }
});

ここで確認できます http://jsfiddle.net/JRLn9/2/

于 2012-06-26T20:45:01.820 に答える
1
var tipo = $('#elemento14').attr('type');
于 2012-06-26T20:45:06.657 に答える