使用 jquery 导航 dom 以获取特定元素
我试图使用 jquery 的父母/兄弟姐妹来查找特定的输入元素,但我似乎无法做到这一点。
我有以下 HTML:
<div id="ExtrasOptions">
<div class="selectItem">
<div class="selectPrice"><span>Qty: <input name="qty" type="text" value="0" maxlength="2" id="qty" class="AccessoryQuantity" /></span></div>
<div class="selectIt"><span><input name="extraselected" type="checkbox" id="extraselected" value="9" /><label for="extrasID">Add this</label></span></div>
</div>
<div class="selectItem">
<div class="selectPrice"><span>Qty: <input name="qty2" type="text" value="0" maxlength="2" id="qty2" class="AccessoryQuantity" /></span></div>
<div class="selectIt"><span><input name="extraselected2" type="checkbox" id="extraselected2" value="9" /><label for="extrasID">Add this</label></span></div>
</div>
</div>
Q1:当有人选中复选框时,我希望同一 div.selectItem 中的文本框放入“1”。如果他们取消选中该复选框,我希望删除该值。
Q2:我还希望在文本框中输入值时选中该复选框,如果文本框为空则取消选中该复选框。
感谢您的帮助。
I am trying to use jquery's parents/siblings to find particular input elements but I cannot seem to get this right.
I have the following HTML:
<div id="ExtrasOptions">
<div class="selectItem">
<div class="selectPrice"><span>Qty: <input name="qty" type="text" value="0" maxlength="2" id="qty" class="AccessoryQuantity" /></span></div>
<div class="selectIt"><span><input name="extraselected" type="checkbox" id="extraselected" value="9" /><label for="extrasID">Add this</label></span></div>
</div>
<div class="selectItem">
<div class="selectPrice"><span>Qty: <input name="qty2" type="text" value="0" maxlength="2" id="qty2" class="AccessoryQuantity" /></span></div>
<div class="selectIt"><span><input name="extraselected2" type="checkbox" id="extraselected2" value="9" /><label for="extrasID">Add this</label></span></div>
</div>
</div>
Q1: When someone checked a checkbox I want the textbox within the same div.selectItem to have a '1' put into it. If they uncheck the checkbox I want the value to be removed.
Q2: I also want the checkbox to be checked if a value is entered into the textbox and unchecked if the textbox is blank.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的东西应该有效。 (未测试精确语法,但算法很可靠。)
Something like this should work. (Not tested for precise syntax, but the algorithm is solid.)
这适用于您的标记(已测试):
This works with your markup (tested):