[英]How to access an array of input elements created with ajax and javascript and get those individual values
Here's my code and my problem:这是我的代码和我的问题:
HTML: HTML:
<tbody id="list"></tbody>
Javascript: Javascript:
let suggest;
const name = $('#post input[name=name]');
const rating = $('#post input[name=rating]');
const postinputs = $('#form-submit');
const table = document.querySelector('#list');
$.ajax({
method: 'GET',
url: wpApiSettings.root+'top-list-route/my-top-list-get',
contentType: 'application/json; charset=utf-8',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
dataType: 'json',
success: ajaxResponse
});
function ajaxResponse(data) {
let str = [];
suggest = data;
for ( let i = 0; i < suggest.length; i ++ ) {
const name = suggest[i].name;
const rating = suggest[i].rating;
str += '<tr>';
str += `<td><input type="text" value=${suggest[i].name}></td>`;
str += `<td><input type="text" value=${suggest[i].rating}></td>`;
str += `<td><button type="button">Update</button></td>`;
str += `<td><button class="delete">Delete</button><input name="delete[]" type="hidden" value=${suggest[i].id}></td>`;
str += '</tr>';
}
table.innerHTML = str;
}
Essentially I need to access the code of this string:基本上我需要访问这个字符串的代码:
str += `<td><button class="delete">Delete</button><input name="delete[]" type="hidden" value=${suggest[i].id}></td>`;
In particular, I need to access the value, which is dynamically added by javascript.特别是,我需要访问由 javascript 动态添加的值。
I tried:我试过:
$(table).add('.delete').click( function() {
var log = $('input[name="delete[]"]').val();
console.log(log);
}
But the result is always 2, which is the first value of the array obtained from the input array of that particular element.但结果始终为 2,它是从该特定元素的输入数组中获得的数组的第一个值。
How can I solve my problem?我该如何解决我的问题?
I tried also to bind the element with an event (on):我还尝试将元素与事件 (on) 绑定:
$(document).on(click, 'input[name="delete[]"]', function() {
console.log(this.val());
}
But it doesn't return anything.但它不会返回任何东西。
Your second solution, the $(document).on(input)
one, is correct in principle except that your input is hidden, so you cannot click on it.您的第二个解决方案
$(document).on(input)
原则上是正确的,只是您的输入是隐藏的,因此您无法单击它。 The first one is almost right.第一个几乎是对的。 It should look like:
它应该看起来像:
$(table).find('.delete').click( function() {
var log = $(this).siblings('input[name="delete[]"]').val();
console.log(log);
});
The idea is that we bind the click
handler on the button ( ".delete"
), and in the handler we find the input (which we know is a sibling of the button).这个想法是我们在按钮上绑定
click
处理程序( ".delete"
),并在处理程序中找到输入(我们知道它是按钮的兄弟)。
There's probably another problem in your case, though: if you load the table elements dynamically after, they won't have the click handler bound.不过,您的情况可能还有另一个问题:如果您之后动态加载表格元素,它们将不会绑定单击处理程序。 So you could go with a compromise:
所以你可以妥协:
$(document).on('click', '.delete', function() {
var log = $(this).siblings('input[name="delete[]"]').val();
console.log(log);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.