My code is here and html repeats itself i have a more than one button.When I click the button, I want it to return the value of the hidden input on the line where the button is located, but the buttons return the value in the first line
<div id="up-div">
<input type="hidden" name="producecode" id="pcode" data-name="{{per.produceCode}}" value="{{per.produceCode}}" />
<input type="hidden" name="useremail" value="{{user.username}}" />
<button ><i ></i></button>
</div>
and jquery
$(".up").click(function(){
console.log($("#pcode").val());
console.log($(".uponequantity").val());
$.ajax({
type: "POST",
url: "/uponequantity/",
headers: { "X-CSRFToken": '{{csrf_token}}' },
data: {"producecode":$("#pcode").val(),
"useremail":$(".uponequantity").val(),
},
success: function() {
console.log('success');
}
});
})
CodePudding user response:
Just add another line to reference your second input same as the first.
$(".up").click(function(){
$(".prdtd").find("input:first- child").attr("value");
$(".pcode").val()
$(".uponequantity").val()//<-- new line for the second hidden value
}
-----------------------------------------
Update:
-----------------------------------------
OP has multiple buttons with the same class each in it's own div and wants to access the values within each div.
So, first off you need to change "pcode" from an id to class in order to get all instances. (id is for unique instances)
After that you need to be able to get the "pcode" & "uponequantity" that are associated with that button and one way of doing that is:
$(".up").click(function(){
$(this).parent().find(".pcode").val()
$(this).parent().find(".uponequantity").val()
}
Simply put, the this
keyword references the clicked button.
You then get the direct parent element which would have the button and the hidden values as children.
Finally you get each specific child through the find
command and do whatever you want with them.
CodePudding user response:
If I understand your question correctly, you would like to get the value of both inputs in your callback.
It seems like you misspelt pdcode
in your JavaScript.
Does the below code snippet help?
$(".up").click(function() {
const value1 = $(".pdcode").val();
const value2 = $(".uponequantity").val();
}