gpt4 book ai didi

javascript - 从动态页面/表中获取数据?

转载 作者:行者123 更新时间:2023-11-29 11:37:02 25 4
gpt4 key购买 nike

创建动态页面是为了显示从数据库获取的数据,动态页面还创建一个表来显示数据。

然后,我在每组结果下方都有一个按钮(btnJoin),该按钮会触发更新表格的函数。

为了更新表,我需要获取用户加入的特定事件,因此是否可以从按钮所在的特定事件的标题(response.rows.item(i).Title)获取数据按下?

更新函数的 SQL 查询应类似于:

tx.executeSql("Update soccerevents SET NoPeople  = NoPeople +1 WHERE Title = '" + response.rows.item(i).Title + "'", [], successJoin,errorCB);

JavaScript(动态页面/表格):

function fetchEvent() {
db = window.openDatabase("SoccerEarth", "2.0", "SoccerEarthDB", 2 * 1024 * 1024);
db.transaction(function(tx) {
var TitleT = document.getElementById("texttitle").value;
tx.executeSql("SELECT * FROM SoccerEvents WHERE Title LIKE '%" + TitleT + "%'", [], successCBValue, errorCB);
});
function successCBValue(tx, response, page_id) {
var formElements = "<table id='resulttable' data-role='table' data-mode='reflow' class='ui-responsive table-stroke table-stripe'><thead><tr><th>Title</th><th>Location</th><th>NoPeople</th><th>Date</th><th>Description</th></tr></thead><tbody>";
for (var i = 0; i < response.rows.length; i++) {
formElements += "<tr><td>"
+ response.rows.item(i).Title + "</td><td>"
+ response.rows.item(i).Location +"</td><td>"
+ response.rows.item(i).NoPeople + "</td><td>"
+ response.rows.item(i).Date +"</td><td>"
+ response.rows.item(i).Description + "<button data-rowtitle='" + response.rows[i].Title + "' data-mini='true' class='btnJoin'>Join</a></td></tr>";
}
formElements+="</tbody></table>";
alert("For better viewing of events, tilt phone landscape.");
$('#page_body').append('<div data-role="page" data-theme="d" id="' + page_id + '"><div data-role="content">' + formElements + '<a href="#page4" data-role="button" data-mini="true">Return</a></div></div>');
$.mobile.initializePage();
$.mobile.changePage("#" + page_id);
$(document).on("click", ".btnJoin", function(e){
var title = $(this).data("rowtitle");
alert(title);
});
}
}
function updateEvent() {
var title = document.getElementById("rowtitle");
db = window.openDatabase("SoccerEarth", "2.0", "SoccerEarthDB", 2 * 1024 * 1024);
db.transaction(function(tx) {
tx.executeSql("Update soccerevents SET NoPeople = NoPeople +1 WHERE Title = '" + title + "'", [], successJoin,errorCB);
});
function successJoin() {
navigator.notification.alert("You have joined the event!", null, "Information", "ok");
$( ":mobile-pagecontainer" ).pagecontainer( "change", "#page4" );
}
}

最佳答案

您可以将标题添加为按钮的数据属性

for (var i = 0; i < response.rows.length; i++) {
formElements += "<tr><td>" + response.rows[i].Title + "</td><td>" +
response.rows[i].Location +"</td><td>" +
response.rows[i].NoPeople + "</td><td>" +
response.rows[i].Date +"</td><td>" +
response.rows[i].Description +
"<button data-rowtitle='" + response.rows[i].Title + "' class='btnJoin' >Click me</button></td></tr>";
}

然后在按钮单击处理程序中,使用 jQuery data() 方法检索属性:

$(document).on("click", ".btnJoin", function(e){
var title = $(this).data("rowtitle");
alert(title);
});

<强> DEMO

关于javascript - 从动态页面/表中获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36404325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com