第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么我的 JS 函數(shù)參數(shù)未定義?

為什么我的 JS 函數(shù)參數(shù)未定義?

DIEA 2023-03-10 14:53:21
我有一個接受好友請求的腳本加載,我的 onclick 函數(shù)是:onclick="acceptfriendrequest('John');"更多細節(jié) :echo  "<br><div style='width:100%; height: 30px;'><div class='userpic' style='background-image: url($avatar); margin-left: 10px; float: left;'></div><p class='incomingfriendrequests' style='float: left; margin-top: 7px; margin-left: 8px;'>$usernames</p><input type='submit' onclick='acceptfriendrequest($usernames)' style= 'margin-left: 10px; margin-top: 6.5px; float: left;' name='accept' value='Accept'><input type='submit' style= 'margin-left: 10px; margin-top: 6.5px; float: left;' name='deny'onclick='declinefriendrequest($usernames)'  value='Deny'></div>";但出于某種原因,我的控制臺顯示:Uncaught ReferenceError: John is not defined at HTMLInputElement.onclick。這是它鏈接到的功能:function acceptfriendrequest(username) {  var fd = new FormData();  fd.append("username", username);  fd.append("accept", true);  var xhr = new XMLHttpRequest();  var fullurl = '../backend/friends.php';    xhr.open('POST', fullurl, true);    xhr.onload = function() {      if (this.status == 200) {        loadfriends();      };  };  xhr.send(fd);}請幫助
查看完整描述

2 回答

?
慕碼人8056858

TA貢獻1803條經(jīng)驗 獲得超6個贊

在開發(fā)工具中查看由 PHP 生成的代碼,它給出了這個(假設 $usernames='John' 在 PHP 中,并且只是一個名字,而不是一個數(shù)組)


<br><div style='width:100%; height: 30px;'><div class='userpic' style='background-image: url(); margin-left: 10px; float: left;'></div><p class='incomingfriendrequests' style='float: left; margin-top: 7px; margin-left: 8px;'>John</p><input type='submit' onclick='acceptfriendrequest(John)' style= 'margin-left: 10px; margin-top: 6.5px; float: left;' name='accept' value='Accept'><input type='submit' style= 'margin-left: 10px; margin-top: 6.5px; float: left;' name='deny'onclick='declinefriendrequest(John)'  value='Deny'></div>)


查看接受好友請求和拒絕好友請求的調用。他們那里有 John,而不是字符串,而且它需要是一個字符串,正如一些評論員以某種方式指出的那樣。


需要更改的是 PHP,onclick='acceptfriendrequest($usernames)' 需要添加引號。


這是修改后的 PHP 回顯——我將元素拆分為換行符以使事情更清楚,并以基本方式添加必要的引號以顯示正在發(fā)生的事情:


echo  "<br><div style='width:100%; height: 30px;'>

        <div class='userpic' style='background-image: url($avatar); margin-left: 10px; float: left;'></div>

        <p class='incomingfriendrequests' style='float: left; margin-top: 7px; margin-left: 8px;'>$usernames</p>

        <input type='submit' onclick='acceptfriendrequest(".'"'.$usernames.'"'.")' style= 'margin-left: 10px; margin-top: 6.5px; float: left;' name='accept' value='Accept'>

        <input type='submit' style= 'margin-left: 10px; margin-top: 6.5px; float: left;' name='deny'onclick='declinefriendrequest(".'"'.$usernames.'"'.")'  value='Deny'>

        </div>";


查看完整回答
反對 回復 2023-03-10
?
呼喚遠方

TA貢獻1856條經(jīng)驗 獲得超11個贊

我希望這對你來說很清楚:)


在 HTML onclick 中,您需要放置不帶任何參數(shù)或 () 的函數(shù):


 // function without () or arguments

 onclick=acceptfriendrequest

在 JavaScript 中使變量中的參數(shù)超出函數(shù)范圍,然后在函數(shù)中使用此變量作為參數(shù):


// variables as argument     

let username = "Name";


// function without arguments

function acceptfriendrequest() {


  // use variables in function :)

  var fd = new FormData();

  fd.append("username", username);

  fd.append("accept", true);

  var xhr = new XMLHttpRequest();

  var fullurl = '../backend/friends.php';

  xhr.open('POST', fullurl, true);

  xhr.onload = function() {

    if (this.status == 200) {

      loadfriends();

    };

};


查看完整回答
反對 回復 2023-03-10
  • 2 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號