英文:
Button only returns data on second click
问题
以下是翻译好的内容,包括你提供的代码部分:
我有以下代码来返回所需的数据:
```html
<button type="button" tabindex="0" class="dropdown-item btn-show dad-pagamento" >Teste</button>
$(document).on('click', '.dad-pagamento', function() {
$.getJSON('valreceber.php', function(data) {
$.getJSON('valreceberext.php', function(data1) {
$.getJSON('valreceberext1.php', function(data2) {
$.getJSON('valreceberext2.php', function(data3) {
$.getJSON('valreceberext3.php', function(data4) {
$.getJSON('valreceberext4.php', function(data5) {
var results = data.concat(data1).concat(data2).concat(data3).concat(data4).concat(data5);
var arr = [];
Object.keys(results).forEach(c => {
id_utt1 = results[c].N_utente;
arr.push(id_utt1);
})
$.getJSON('valreceber1.php?arr' + '&arr=' + arr, function(data6) {
document.querySelector(".dad-pagamento").addEventListener("click", clickHandler);
function clickHandler() {
data6.sort(function(a, b) {
return a.N_utente - b.N_utente;
});
data6.forEach(function(person) {
var id = person.N_utente;
var filteredData = results.filter(d => d.N_utente === id);
addHtml(person.NomeResp, person.moradaResp, person.CodigoPostal, person.Localidade, filteredData);
});
}
function addHtml(NomeResp, moradaResp, CodigoPostal, Localidade, filteredData) {
var html = ``;
html += ``;
document.querySelector(".pagmfalta").innerHTML += html;
$('#minhaDiv1').show();
}
});
});
});
});
});
});
});
});
在期望的结果方面,它正常工作。
我遇到的问题是按钮调用函数返回数据的问题。为了显示数据,我必须点击两次按钮,我希望点击按钮一次就能显示来自数据库的数据。
只有在第二次点击时显示数据的原因是这些代码:
$(document).on('click', '.dad-pagamento', function(){
和
document.querySelector(".dad-pagamento").addEventListener("click", clickHandler);
如何解决点击一次按钮立即显示数据的问题?
英文:
I have the following code to return the desired data:
<button type="button" tabindex="0" class="dropdown-item btn-show dad-pagamento" >Teste</button>
$(document).on('click', '.dad-pagamento', function() {
$.getJSON('valreceber.php', function(data) {
$.getJSON('valreceberext.php', function(data1) {
$.getJSON('valreceberext1.php', function(data2) {
$.getJSON('valreceberext2.php', function(data3) {
$.getJSON('valreceberext3.php', function(data4) {
$.getJSON('valreceberext4.php', function(data5) {
var results = data.concat(data1).concat(data2).concat(data3).concat(data4).concat(data5);
var arr = [];
Object.keys(results).forEach(c => {
id_utt1 = results[c].N_utente;
arr.push(id_utt1);
})
$.getJSON('valreceber1.php?arr' + '&arr=' + arr, function(data6) {
document.querySelector(".dad-pagamento").addEventListener("click", clickHandler);
function clickHandler() {
data6.sort(function(a, b) {
return a.N_utente - b.N_utente;
});
data6.forEach(function(person) {
var id = person.N_utente;
var filteredData = results.filter(d => d.N_utente === id);
addHtml(person.NomeResp, person.moradaResp, person.CodigoPostal, person.Localidade, filteredData);
});
}
function addHtml(NomeResp, moradaResp, CodigoPostal, Localidade, filteredData) {
var html = ``;
html += ``;
document.querySelector(".pagmfalta").innerHTML += html;
$('#minhaDiv1').show();
}
});
});
});
});
});
});
});
});
At the level of expected result, it is working correctly.
What I have is a problem with the button that calls the function to return the data. For the data to be shown, I have to click the button twice and I wanted to click the button once to show the data coming from the database.
The reason for showing the data only on the second click is because of these lines:
$(document).on('click', '.dad-pagamento', function(){
and
document.querySelector(".dad-pagamento").addEventListener("click", clickHandler);
How can I solve the problem for when clicking once on the button show the data soon?
答案1
得分: 1
看起来你只需要手动调用处理程序,这样它就会附加在第一次点击上:
$(document).on('click', '.dad-pagamento', function() {
// ...
function clickHandler() {
// ....
}
clickHandler(); // 立即应用它一次
document.querySelector(".dad-pagamento").addEventListener("click", clickHandler); // 将它设置为以后的点击处理程序
// ...
});
(请注意,我将 clickHandler
放在其声明之后的行上 - 这种方式也可以,但会令人困惑)
英文:
Looks like you just have to invoke the handler manually, so that it piggy-backs on the first click:
$(document).on('click', '.dad-pagamento', function() {
// ...
function clickHandler() {
....
}
clickHandler(); // apply it once right away
document.querySelector(".dad-pagamento").addEventListener("click", clickHandler); // set it as handler for later clicks
// ...
});
(Note that I moved the line where the clickHandler
is placed after its declaration - it works the other way too, but it is confusing)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论