微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何识别孩子的输入类型

如何解决如何识别孩子的输入类型

| 基本上我有几个div,每个div都包含一个问题。这个问题可以用单个输入,单选按钮或复选框来表示。 在遍历div时,我想确定输入类型是什么。 以下代码无法实现此目的,只是试图演示我正在尝试实现的目标。
$(\".question\").each(function(){

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

解决方法

        删除val():
  var type = $(this).children(\'input\').attr(\"type\");
例如
  $(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>
    ,        现场演示
$(\".question > input\").each(function(){
        var type = $(this).attr(\"type\");
        alert(type);
});
    ,        
var type = $(this).find(\"input\").get(0).type;
    ,        这应该可以帮助您:
$(\'div\').each(function(){
    $(this).children(\'input\').each(function(){
       alert($(this).attr(\'type\'));   
    });
});
小提琴:http://jsfiddle.net/p6F9T/     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。