【发布时间】:2018-09-20 13:17:00
【问题描述】:
我正在尝试将'for' 属性分配给每个标签标记,而不是手动键入它。另外,我还想让每个'for'等于它对应的输入'name'
如何使用 Jquery 编写代码来动态更改每个代码?
下面是我的html。
我试过这样做:
$("input, select").each(function(){
$(this).siblings().attr('for', [input=name].val)
});
但这不起作用。有任何想法吗?
$("input, select").each(function(){
$(this).siblings().attr('for', [input=name].val)
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="billingArea">
<p>
<label>First Name:</label>
<input name="billingFirstName" type="text" value="" required />
</p>
<p>
<label>Last Name:</label>
<input name="billingLastName" type="text" value="" required />
</p>
<p>
<label>Street Address:</label>
<input name="billingAddress1" type="text" value="" required />
</p>
<p>
<label> Street Address 2: </label>
<input name="billingAddress2" type="text" value="" />
</p>
<p>
<label> City: </label>
<input name="billingCity" type="text" value="" required />
</p>
<p>
State/Province: <select name="billingState" required />
<option value="">--</option>
<option value="Alberta, Canada">AB</option>
<option value="Alaska, USA">AK</option>
</select>
</p>
<p>
<label>Postal Code:</label> <input name="billingZip" type="text" value="" required />
</p>
</div>
【问题讨论】:
-
只是为了确保你知道,你知道如果你把输入 inside 标签,你不需要 for 属性,对吧?
-
另外
State/Province是一只不在标签标签中的奇怪鸭子。
标签: javascript jquery for-loop each attr