我想要一个包含表单中所有可用标签的列表,或者只是一种遍历所有标签的方法,然后逐一检查文本。
我先试了:
$('label').each(function() {
console.log(this); // this was to test :/
});
以及后来的一些变体:
$('input').labels().each(function() { ... });
$('label').siblings().each(function() { ... });
没有对这种形式起作用:
<form class="form-group">
<label for="id_name">Name:</label>
<input type="text" name="name" maxlength="255" class="form-control" required="" id="id_name">
<label for="id_cnpj">Cnpj:</label>
<input type="text" name="cnpj" maxlength="14" class="form-control" required="" id="id_cnpj">
<label for="id_cpf">Cpf:</label>
<input type="text" name="cpf" maxlength="11" class="form-control" required="" id="id_cpf">
<label for="id_rg">Rg:</label>
<input type="text" name="rg" maxlength="14" class="form-control" required="" id="id_rg">
<label for="id_federation_unity">Federation unity:</label>
<select name="federation_unity" class="form-control" required="" id="id_federation_unity">
and much more ....
</form>
引用文献: .labels()
, .siblings()
我该怎么做?
编辑 我在这里犯了一个很大的菜鸟错误,我很抱歉。 我发现出了什么问题。 我应该把整个代码放在第一位:
<!doctype html>
<html>
<body>
<form class="form-group">
<label for="id_name">Name:</label>
<input type="text" name="name" maxlength="255" class="form-control" required="" id="id_name">
<label for="id_cnpj">Cnpj:</label>
<input type="text" name="cnpj" maxlength="14" class="form-control" required="" id="id_cnpj">
<label for="id_cpf">Cpf:</label>
<input type="text" name="cpf" maxlength="11" class="form-control" required="" id="id_cpf">
<label for="id_rg">Rg:</label>
<input type="text" name="rg" maxlength="14" class="form-control" required="" id="id_rg">
<label for="id_federation_unity">Federation unity:</label>
<select name="federation_unity" class="form-control" required="" id="id_federation_unity">
</form>
<script src="jquery-3.2.1.min.js" rel="javascript"></script>
<script type="javascript">
$('label').each(function() {
console.log(this);
});
</script>
</body>
</html>
问题
我改变了<script type="javascript">jquery code here</script>
通过删除 type="javascript"
属性,它起作用了。我认为 type="javascript"
的用法只有在 <script>
时才可行标记在 <head>
中定义标签。
另一件事:我测试了版本 $('label').siblings().each(function() { ... });
而且效果也很好。
非常感谢你们花时间和精力帮助我解决第一个提交的问题。
最佳答案
选择 form-group
类和 label
并使用 each
进行迭代,.text()
方法将返回标签的文本内容。 trim()
删除任何空白
$(".form-group label").each(function(i, v) {
console.log($(v).text().trim())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form-group">
<label for="id_name">Name:</label>
<input type="text" name="name" maxlength="255" class="form-control" required="" id="id_name">
<label for="id_cnpj">Cnpj:</label>
<input type="text" name="cnpj" maxlength="14" class="form-control" required="" id="id_cnpj">
<label for="id_cpf">Cpf:</label>
<input type="text" name="cpf" maxlength="11" class="form-control" required="" id="id_cpf">
<label for="id_rg">Rg:</label>
<input type="text" name="rg" maxlength="14" class="form-control" required="" id="id_rg">
<label for="id_federation_unity">Federation unity:</label>
<select name="federation_unity" class="form-control" required="" id="id_federation_unity"></select>
</form>
关于javascript - 如何获取包含表单中可用的所有输入标签的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50302822/