2017-07-17 7 views
0

入力IDを渡して対応するタイプを取得しようとしています。IDを渡して入力タイプを取得

例:だから、 "1" 日付とパッシングを返す渡す

<input id = "1" type = "date"/> 
<input id = "2" type = "text"/> 

"2" テキストを返します。

私は、私の場合のために働くいくつかの例が、どれも見てきました:

How to use jquery selector having element type and ID

finding the type of an element using jQuery

私がこれまで持っている:

$("#my_select").on('click',function(){ 
     var fieldID = document.querySelector('.selected').id 
     console.log("id is " + fieldID); 
     var elementType = $(this).prev().prop('.selected'); 
     console.log("element type " + elementType) 
}); 
+0

のような他のタグのラジオテキストメールやタグ名のように、あなたの入力のためのタイプを与える文書全体で一意のIDがありますか? – juvian

答えて

5
var type = $("#id").attr("type"); 

万一それをやる。

1

このようにすることができます。

$("#my_select").on('click',function(){ 
      var fieldID = document.querySelector('.selected').id 
       console.log("id is " + fieldID); 
      var elementType = $('#' + fieldID).prop('type'); 
      console.log("element type " + elementType) 

     }); 
0

ここでは、タイプをあなたにお返しします入力オンソリューションhttps://jsfiddle.net/bpq58x8m/

getType = function(id){ 
 
\t console.log($('#' + id).attr('type')); 
 
} 
 

 
$('input').click(function(){ 
 
\t getType($(this).attr('id')); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id = "1" type = "date"/> 
 
<input id = "2" type = "text"/>

をクリックで行きます。 参考のためだけに余分な機能を追加しましたが、それを無視して2つを1つにまとめることができます。

1

考えられる解決策。

$("#my_select").on('change', function() { 
 
    var fieldID = document.getElementById($(this).val()); 
 
    console.log(`id is ${fieldID.id} and type is ${fieldID.type}`); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input id="1" type="date" /> 
 
<input id="2" type="text" /> 
 
<select id='my_select'> 
 
    <option value='1'>1</option> 
 
    <option value='2'>2</option> 
 
</select>

-1
$("#my_select").on('click',function(){ 
    var fields=$("#your-Form-Id").children(); 
    for(i=0;i<fields.length;i++){ 
     var type = field[i].type || field[i].tagName.toLowerCase(); 
     alert(type); 
    } 
}); 

ここでは、テキストエリア

関連する問題