如何识别儿童输入类型

时间:2011-05-27 14:00:53

标签: jquery forms

基本上我有几个div,每个包含一个问题。这个问题可以通过单个输入,单选按钮或复选框呈现。

我想在循环遍历div时确定输入类型是什么。

以下代码实现此功能不起作用,只是试图展示我想要实现的目标。

$(".question").each(function(){

        var type = $(this).children('input').attr("type").val();
        alert(type);
});

4 个答案:

答案 0 :(得分:1)

<强> Live Demo

$(".question > input").each(function(){
        var type = $(this).attr("type");
        alert(type);
});

答案 1 :(得分:1)

删除val():

  var type = $(this).children('input').attr("type");

e.g。

  $(document).ready(function(){
      $('button').click(function(){$(".question").each(function(){

         var type = $(this).children('input').attr("type");
         alert(type);
        });
      });
  });


 <div class="question">
  <input type="radio">hello</input>
 </div>

 <div class="question">
   <input type="checkbox">hello</input>
 </div>

 <div class="question">
   <input type='text' name='response[4][answer]'></input>
 </div>

 <button>Click</button>

答案 2 :(得分:1)

var type = $(this).find("input").get(0).type;

答案 3 :(得分:0)

这应该让你前进:

$('div').each(function(){
    $(this).children('input').each(function(){
       alert($(this).attr('type'));   
    });
});

小提琴:http://jsfiddle.net/p6F9T/

相关问题