使用 jquery 导航 dom 以获取特定元素

发布于 2024-08-03 17:28:46 字数 1130 浏览 2 评论 0原文

我试图使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

假面具 2024-08-10 17:28:46

像这样的东西应该有效。 (未测试精确语法,但算法很可靠。)

// Bind an event to each of your checkboxes, for when they are clicked
$("input[type=checkbox]").click(function() {
  if ($(this).attr("checked")) {
    // The checkbox is checked, so find the associated text input and change its value
    $(this).parents(".selectItem").find("input[type=text]").val("1");
  } else {
    // The checkbox is unchecked, so find the associated text input and remove its value
    $(this).parents(".selectItem").find("input[type=text]").val("");
  }
});

// Bind an event to each of your text inputs, for when they have text entered into them
$("input[type=text]").keypress(function() {
  if ($(this).val() != "")) {
    // There is something in the text input box, so check the associated checkbox
    $(this).parents(".selectItem").find("input[type=checkbox]").attr("checked","checked");
  } else {
    // There is nothing in the text input box, so uncheck the associated checkbox
    $(this).parents(".selectItem").find("input[type=checkbox]").attr("checked","");
  }
});

Something like this should work. (Not tested for precise syntax, but the algorithm is solid.)

// Bind an event to each of your checkboxes, for when they are clicked
$("input[type=checkbox]").click(function() {
  if ($(this).attr("checked")) {
    // The checkbox is checked, so find the associated text input and change its value
    $(this).parents(".selectItem").find("input[type=text]").val("1");
  } else {
    // The checkbox is unchecked, so find the associated text input and remove its value
    $(this).parents(".selectItem").find("input[type=text]").val("");
  }
});

// Bind an event to each of your text inputs, for when they have text entered into them
$("input[type=text]").keypress(function() {
  if ($(this).val() != "")) {
    // There is something in the text input box, so check the associated checkbox
    $(this).parents(".selectItem").find("input[type=checkbox]").attr("checked","checked");
  } else {
    // There is nothing in the text input box, so uncheck the associated checkbox
    $(this).parents(".selectItem").find("input[type=checkbox]").attr("checked","");
  }
});
娇纵 2024-08-10 17:28:46

这适用于您的标记(已测试):

$(document).ready(function() {
    $('input:checkbox').change(function() {
        if($(this).is(':checked')) {
            $(this).val('1');
        } else {
            $(this).val('');
        }
    });

    $('input[type=text]').keyup(function() {
        if($(this).val() != "") {
            $(this).parent()
                   .parent()
                   .next('.selectIt')
                   .find('span > input:checkbox')
                   .attr('checked','checked')
                   .val('1');
        } else {
            $(this).parent()
                   .parent()
                   .next('.selectIt')
                   .find('span > input:checkbox')
                   .removeAttr('checked')
                   .val('');
        }
    });
}); 

This works with your markup (tested):

$(document).ready(function() {
    $('input:checkbox').change(function() {
        if($(this).is(':checked')) {
            $(this).val('1');
        } else {
            $(this).val('');
        }
    });

    $('input[type=text]').keyup(function() {
        if($(this).val() != "") {
            $(this).parent()
                   .parent()
                   .next('.selectIt')
                   .find('span > input:checkbox')
                   .attr('checked','checked')
                   .val('1');
        } else {
            $(this).parent()
                   .parent()
                   .next('.selectIt')
                   .find('span > input:checkbox')
                   .removeAttr('checked')
                   .val('');
        }
    });
}); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文