/ / कैसे jquas या जावास्क्रिप्ट का उपयोग करके nth div के चाइल्ड एलिमेंट वैल्यू प्राप्त करें - जावास्क्रिप्ट, jquery

Jquery या javascript - javascript, jquery का उपयोग करके nth div के चाइल्ड एलिमेंट वैल्यू को कैसे प्राप्त करें

मैं 4th div के चाइल्ड इनपुट एलिमेंट की वैल्यू पाना चाहता हूं। मूल रूप से जाँच की जा रही chekbox मान im और "यह" कीवर्ड का उपयोग कर संबंधित इनपुट बॉक्स मूल्य जोड़ना चाहते हैं यहाँ मेरा html कोड है

<div class="container">
<div class="cell-checkbox" style="float:left;margin-right:5%;">
<input type="checkbox" name="select" value="7" class="big">
</div>
<div class="cell-desc" style="float:left;margin-right:5%;width:30%;">
<!-- Content -->
</div>
<div class="cell-cart"> //input box is inside this div
<table cellpadding="0" cellspacing="0" border="0">
<tbody>
<tr>
<td align="right">
<table align="center" class="cellBuy">
<tbody>
<tr align="right">
<td width="1%">
<input type="hidden" name="buyid" id="buyid" value="7">
<input type="hidden" name="category" value="464">
<input type="hidden" name="itemid" value="">
<input name="qty" id="qty" size="6" maxlength="6" value="1" class="input"> /*want to get this input text value
&nbsp;
</td>
<td height="20">
<div align="left">
<input type="button" class="btn-BuyOff" value="Buy" id="addtocart" name="addtocart">
</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
</div>

मैंने नीचे दिए गए कोड की कोशिश की है, लेकिन इसके काम नहीं कर रहा है

var result = [];
$(":checkbox:checked").each(function (i) {
result.push($(this).val() + "," + $(this).parent().next().find("#qty").val());// this is for finding quantity field value
});
var strreuslt = result.join(";");
alert(strreuslt);

उत्तर:

जवाब के लिए 2 № 1

निकटतम अभिभावक को प्राप्त करने के लिए निकटतम का उपयोग करने का प्रयास करें जिसमें दोनों इनपुट होते हैं और खोज को ट्रिगर करते हैं

$(":checkbox:checked").each(function (i) {
result.push($(this).val() + "," + $(this).closest(".container").find("input[name="qty"]").val());
});

या:

  $(":checkbox:checked").each(function (i) {
result.push($(this).val() + "," + $(this).parent().siblings(".cell-cart").find("input[name="qty"]").val());
});

नोट: यदि आपके पास इनपुट के कई समूह हैं, तो आपको प्रत्येक समूह को अधिमानतः एक तत्व में लपेटना होगा ul li


जवाब के लिए 2 № 2

$(":checkbox").eq(3) यह आपको 4 तत्व देता है jquery। इंडेक्सिंग शून्य से शुरू होती है, इसलिए .eq (3) 4 वां बच्चा देगा।


उत्तर № 3 के लिए 1

उपयोग name इसके बजाय, और कभी भी समान का उपयोग न करें id कई तत्वों के लिए:

$(this).parent().siblings(".cell-cart").find("[name=qty]").val();

या कंटेनर कई होते हैं तो इस का उपयोग करें <div class="cell-cart">:

$(this).parent().next().next().find("[name=qty]").val();

उत्तर के लिए 1 № 4

आपके पास डोम ट्रैवर्सिंग का मुद्दा है। $(this).parent() इसमें इनपुट चेकबॉक्स तत्व वाले div वापस न करें। आपको क्लास कंटेनर के साथ निकटतम डिव के लिए पार करना चाहिए और फिर इसमें इनपुट चेकबॉक्स ढूंढना चाहिए। इस कदर:

var result = [];
$(":checkbox:checked").each(function (i) {
result.push($(this).val() + "," + $(this).closest(".container").find("[name="qty"]").val());// this is for finding quantity field value
});
var strreuslt = result.join(";");
alert(strreuslt);