我的目標是通過 Bootstrap 切換按鈕在 MySQL DB 上寫入“是”或“否”。我知道 JavaScript 在客戶端,所以我找到了一種方法(但不確定是最好的)從 JavaScript 收集變量并將其傳遞給將寫入數據庫的 PHP 文件。我的相關代碼如下,但如果我嘗試,什么也不會發生。我想在我的 JavaScript 代碼中我必須調用“function saveStato”,但我無法理解以哪種方式。$(function() { $('.inServizio').change(function() { if($(this).prop('checked')){ console.log('TEST_ON') function saveStato() { $.post("inServizio.php", { stato: $("SI").val(), }, function(data,status){ document.getElementById("saveWarningText").innerHTML = data; $( "#saveWarningText" ).fadeIn(100); setTimeout(function(){ $( "#saveWarningText" ).fadeOut(100); }, 3000); }); } } else { console.log('TEST_OFF') } //$(this).prop('checked') })});
1 回答

心有法竹
TA貢獻1866條經驗 獲得超5個贊
您可以在更改偵聽器外部定義它,并在內部調用它
saveStato();
或者干脆完全去掉這個功能,然后就可以了
$('.inServizio').change(function() {
if($(this).prop('checked')){
console.log('TEST_ON')
$.post("inServizio.php",{stato: $("SI").val()},function(data,status){
document.getElementById("saveWarningText").innerHTML = data;
$( "#saveWarningText" ).fadeIn(100);
setTimeout(function(){ $( "#saveWarningText" ).fadeOut(100); }, 3000);
});
} else {
//rest of your code
}
- 1 回答
- 0 關注
- 160 瀏覽
添加回答
舉報
0/150
提交
取消