I have many pair of text fields and submit button, with id
to submit button and class
to text field as same but different for each pair. So I want to pass the value entered in text field after button click to ajax function.
function update_rate(id){
// var price = document.getElementsByClassName(id)[0].innerHTML;
var price = document.getElementsByClassName(id);
console.log(price);
$.ajax({
type: "POST",
url: "update_rate.php", // Name of the php files
data: {subcategory : id , price: price},
success: function(res)
{
// console.log(html);
alert(res);
}
});
}
first pair:
<div class="form-group">
<input type="text" class="form-control" name="a" placeholder="Your task rate excl. taxes">
</div>
<button type="submit" id="a" onclick="update_rate(this.id)" class="btn btn-primary">Update</button>
<div class="form-group">
<input type="text" class="form-control" name="b" placeholder="Your task rate excl. taxes">
</div>
<button type="submit" id="b" onclick="update_rate(this.id)" class="btn btn-primary">Update</button>
I have many pair of text fields and submit button, with id
to submit button and class
to text field as same but different for each pair. So I want to pass the value entered in text field after button click to ajax function.
function update_rate(id){
// var price = document.getElementsByClassName(id)[0].innerHTML;
var price = document.getElementsByClassName(id);
console.log(price);
$.ajax({
type: "POST",
url: "update_rate.php", // Name of the php files
data: {subcategory : id , price: price},
success: function(res)
{
// console.log(html);
alert(res);
}
});
}
first pair:
<div class="form-group">
<input type="text" class="form-control" name="a" placeholder="Your task rate excl. taxes">
</div>
<button type="submit" id="a" onclick="update_rate(this.id)" class="btn btn-primary">Update</button>
<div class="form-group">
<input type="text" class="form-control" name="b" placeholder="Your task rate excl. taxes">
</div>
<button type="submit" id="b" onclick="update_rate(this.id)" class="btn btn-primary">Update</button>
But I can't get the value of text field into variable.
Share Improve this question edited Oct 16, 2020 at 0:05 JaviL 1021 silver badge7 bronze badges asked Oct 15, 2020 at 15:06 InternIntern 391 silver badge6 bronze badges 2- Here id is parameter to function and that is also class name to text fields , so how can I get value entered in text fields? – Intern Commented Oct 15, 2020 at 15:14
- See my answer below for a much more simplified way of doing this. – Scott Marcus Commented Oct 15, 2020 at 15:37
3 Answers
Reset to default 2This can and should be done in a different and more simple manner. Connecting the buttons with their text fields by id
and class
is not a scalable solution and requires constant updating of the code to keep all the naming in sync. Just get the value of the text field that es just prior to the button. This will be easier if you modify your HTML structure so that the text field and the button are in the same div
together.
- Don't use inline event handlers, instead separate your event handling code into JavaScript.
- Set up just a single event handler at a higher DOM element and handle it when it bubbles up to that element. This is called Event Delegation.
- Use data-* attributes to store custom data in elements.
- Also, don't use
.getElementsByClassName()
in 2020. Instead, use.querySelector
.
See ments below.
// Do your event handling in JavaScript, not with inline HTML event handling attributes
// Also, set up just one handler at a parent level of the items that might trigger
// the event (event delegation).
$(".wrapper").on("click", update_rate);
function update_rate(event){
// See if it was a submit button that got clicked
if(event.target.classList.contains("btn")){
// A submit button was pressed.
// Locate the nearest ancestor element that has the form-group class
// (event.target references the actual element that triggered the event).
let formGroup = event.target.closest(".form-group");
// and then, from there, find the first input (which is the one you want).
var input = formGroup.querySelector("input");
// The following code is already added to the success handler below and
// that's where it should be. It's only added here to be able to see the
// effect since the AJAX call won't run in Stack Overflow. The next 3 lines
// should be removed when used for real.
input.classList.add("hidden");
formGroup.querySelector("button").classList.add("hidden");
formGroup.querySelector("span").classList.remove("hidden");
console.log(input.value);
$.ajax({
type: "POST",
url: "update_rate.php", // Name of the php files
// Use the dataset API to extract the custom attribute on the input
data: {subcategory : input.dataset.category , price: input.value},
success: function(res){
alert(res);
// Hide the input and the button and show the updated message
input.classList.add("hidden");
formGroup.querySelector("button").classList.add("hidden");
formGroup.querySelector("span").classList.remove("hidden");
}
});
}
}
.hidden { display:none; }
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
<div class="form-group">
<input type="text" class="form-control" data-category="a" placeholder="Your task rate excl. taxes">
<button type="submit" class="btn btn-primary">Update</button>
<span class="hidden">Updated</span>
</div>
<div class="form-group">
<input type="text" class="form-control" data-category="b" placeholder="Your task rate excl. taxes">
<button type="submit" class="btn btn-primary">Update</button>
<span class="hidden">Updated</span>
</div>
</div>
In the end, you have no id
or unique class
names to have to match up against each other, your HTML is more simplified, and you only have one event handler to set up.
Several things
var price = document.getElementsByClassName(id);
is plural and you need the value, so
var price = document.getElementsByClassName(id)[0].value;
if you must
BUT it is not a class. It is a name
var price = document.getElementsName(id)[0].value;
if you must
but if you have jQuery, why not use it?
Here I take the button ID and find the input by name
I also change to type="button" - you do not want to submit when you use Ajax
$(function() { // on page load
$("#container").on("click","[type=button]",function() { // click on type="button" you can use a Class here too
const id = $(this).attr("id");
const price = $("[name="+id+"]").val(); // get the input by name
console.log(id, price)
if (price) {
$.ajax({
type: "POST",
url: "update_rate.php", // Name of the php files
data: {
subcategory: id,
price: price
},
success: function(res) {
// console.log(html);
alert(res);
}
});
}
})
})
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
first pair:
<div class="form-group">
<input type="text" class="form-control" name="a" placeholder="Your task rate excl. taxes">
</div>
<button type="button" id="a" class="btn btn-primary">Update</button>
<div class="form-group">
<input type="text" class="form-control" name="b" placeholder="Your task rate excl. taxes">
</div>
<button type="button" id="b" class="btn btn-primary">Update</button>
</div>
As you're already passing the id in the function, using jQuery, you can do something like:
var selector = 'input[name="' + id + '"]' // Compose the selector string for jQuery
var value = $(selector).val() // Get input value
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745582505a4634337.html
评论列表(0条)