Prevent form submission on Enter key press

前端 未结 19 1963
失恋的感觉
失恋的感觉 2020-11-22 04:18

I have a form with two text boxes, one select drop down and one radio button. When the enter key is pressed, I want

19条回答
  •  误落风尘
    2020-11-22 04:51

    native js (fetch api)

    document.onload = (() => {
        alert('ok');
        let keyListener = document.querySelector('#searchUser');
        // 
        keyListener.addEventListener('keypress', (e) => {
            if(e.keyCode === 13){
                let username = e.target.value;
                console.log(`username = ${username}`);
                fetch(`https://api.github.com/users/${username}`,{
                    data: {
                        client_id: 'xxx',
                        client_secret: 'xxx'
                    }
                })
                .then((user)=>{
                    console.log(`user = ${user}`);
                });
                fetch(`https://api.github.com/users/${username}/repos`,{
                    data: {
                        client_id: 'xxx',
                        client_secret: 'xxx'
                    }
                })
                .then((repos)=>{
                    console.log(`repos = ${repos}`);
                    for (let i = 0; i < repos.length; i++) {
                         console.log(`repos ${i}  = ${repos[i]}`);
                    }
                });
            }else{
                console.log(`e.keyCode = ${e.keyCode}`);
            }
        });
    })();

提交回复
热议问题