javascript - HTML - javascript函數未激活

102 5

我正在使用Spotify api開發程序,使用它的示例代碼來熟悉api ,


<form onsubmit="MyKey()">


 Enter playlist ID:<br>


 <input type ="text" id ="MyKey" value ="">


 <input type ="button" value ="submit"/>


 </form>



然後調用這個函數:


 <script>


 function MyKey(){


var playlist = document.getElementById('MyKey');


console.log(playlist);


 function getHashParams() {


 var hashParams = {};


 var e, r = /([^&;=]+)=?([^&;]*)/g,


 q = window.location.hash.substring(1);


 while ( e = r.exec(q)) {


 hashParams[e[1]] = decodeURIComponent(e[2]);


 }


 return hashParams;


 }



 var params = getHashParams();



var access_token = params.access_token,


refresh_token = params.refresh_token,


error = params.error;



console.log(access_token);


console.log(playlist);


document.getElementById('MyKey').addEventListener('submit',function() {


console.log(playlist);



$.ajax({


 url: 'https://api.spotify.com/v1/playlists/'+playlist+'/tracks?fields=items(track.id)',


 type: GET,


 headers: {


 'Authorization': 'Bearer ' + access_token


 },


 success: function(response) {


 console.log(response.items);


 }


 });


 },false);



 }


</script>



當我點擊提交時,控制台上沒有列印,為什麼我的腳本沒有激活?

时间: 原作者:

59 2

錯誤出現在HTML中,而不是JavaScript中,為了通過按鈕提交表單,你需要將類型設置為submit,以便瀏覽器知道調用onsubmit函數。


<form action="#" onsubmit="return MyKey();">


 Enter playlist ID:<br>


 <input type="text" id="MyKey" value="">


 <button type="submit">Submit</button>


</form>



原作者:
...