问题描述
|
基本上我有几个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/