简体   繁体   中英

How to set enter key press for a HTML tag with Javascript

I would like to add an enter key press after the alert(get_approve_2.innerHTML); as the code below.

This means that every time the alert runs, the enter key press then starts after the alert's popup.

$(get_status1).change(function(){ // Get value of Status 1
                //alert($('option:selected', $(this)).text());
                var value = $('option:selected', $(this)).text();
                var get_approve_2 = document.getElementById("ctl00_m_g_ee431d81_6c29_4b13_a29e_884f483a4e68_ff171_ctl00_ctl00_UserField_upLevelDiv");

                if (value == "Approve"){
                    var name_approve_2 = "Receptionist";
                    get_approve_2.innerHTML = name_approve_2;
                    alert(get_approve_2.innerHTML);
                    // Enter key press goes here
                }

                else{
                    //alert("Reject");
                    get_approve_2.innerHTML = " ";
                }
            });

I have tried with this but I don't know how it can be run automatically or be trigged

function runScript(e) {
if (e.keyCode == 13) {
    var tb = document.getElementById("scriptBox");
    eval(tb.value);
    return false;
}
}

If you're using jQuery.You can use which event on key code 13 for the enter key

// Enter key press goes here

$('Your selector').on('keyup', function(e) {
    if (e.which == 13) {
        e.preventDefault();
    }
});

As you said if you want to automatically be trigged

function runScript(e) {
if (e.keyCode == 13) {
    var tb = document.getElementById("scriptBox");
    eval(tb.value);
    return false;
}
} 

Add your function there

            if (value == "Approve"){
                var name_approve_2 = "Receptionist";
                get_approve_2.innerHTML = name_approve_2;
                alert(get_approve_2.innerHTML);
                // Enter key press goes here
                  runScript(13); //run your function there 
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM