gpt4 book ai didi

Javascript 函数仅在页面刷新后有效

转载 作者:行者123 更新时间:2023-11-29 15:44:51 31 4
gpt4 key购买 nike

如果我点击“发送”按钮,一条消息将被放入客户端 sql 数据库中并显示一条通知。

但是,这只有在我先刷新页面时才有效!

如何让函数立即开始工作?我已经尝试将函数“addMessage”放在 $(document).ready(function() 中,但随后该函数完全停止工作。请参见下面的代码:

<script>
var db;

$(document).ready(function() {
//Opens the database
try
{
if (!window.openDatabase) {
alert('Not supported -> Please use a webkit browser');
} else {
var shortName = 'Rvpk';
var version = '1.0';
var displayName = 'The Rvpk databse';
var maxSize = 3072*1024; // = 3MB in bytes 65536
db = openDatabase(shortName, version, displayName, maxSize);
}
}
catch(e)
{
if (e == 2) {
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}return;
}

//If needed creates the messagetable
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS message (id INTEGER PRIMARY KEY, name)')
});
});

//The function addMessage takes the entered message from the message inputfield and places it inside the messagetable
function addMessage(){
var message = $('input:text[name=message]').val();

db.transaction(function (tx) {
tx.executeSql("INSERT INTO message (id, name) VALUES (NULL, ?)", [message]);
});
console.log("Message "+ message +" added to database");

//Shows a notification that sending the message was a success
alert('The message has been sent');

//Update the messages
updateMessages();
}
</script>
</head>

<body>
<?php include("includes/header2.php"); ?>

<div data-role="content">
<form>
<fieldset>
<label id="messagelabel" for="message">Message:</label>
<input id="message" type="text" name="message" value="This can't go on like this">
<button onClick="addMessage()" data-theme="g">Send</button>
</fieldset>
</form>
</div>

最佳答案

请试试这个:

document.ready 仅在页面加载时运行。而是在单独的函数中使用它,并在需要时调用该函数。复制并粘贴以下脚本。

<script>
var db;

function runThis()
{
//Opens the database
try
{
if (!window.openDatabase) {
alert('Not supported -> Please use a webkit browser');
} else {
var shortName = 'Rvpk';
var version = '1.0';
var displayName = 'The Rvpk databse';
var maxSize = 3072*1024; // = 3MB in bytes 65536
db = openDatabase(shortName, version, displayName, maxSize);
}
}
catch(e)
{
if (e == 2) {
alert("Invalid database version.");
} else {
alert("Unknown error "+e+".");
}return;
}

//If needed creates the messagetable
db.transaction(function (tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS message (id INTEGER PRIMARY KEY, name)')
});
}

//The function addMessage takes the entered message from the message inputfield and places it inside the messagetable
function addMessage(){

//call db access function.
runThis();

var message = $('input:text[name=message]').val();

db.transaction(function (tx) {
tx.executeSql("INSERT INTO message (id, name) VALUES (NULL, ?)", [message]);
});
console.log("Message "+ message +" added to database");

//Shows a notification that sending the message was a success
alert('The message has been sent');

//Update the messages
updateMessages();
}
</script>

关于Javascript 函数仅在页面刷新后有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13208305/

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