Good day everyone!
I have a problem merging the codes in one function. (If it's possible).
First:
Here's the code:
function enableRegButton() {
$('#registerExist').prop('disabled', false);
$('#edit').prop('disabled', false);
// regButton execute when Enter key pressed
$(document).unbind("keyup").keyup(function(e){
var code = e.which; // recommended to use e.which, it's normalized across browsers
if(code==13)
{
$("#registerExist").click();
}
});
}
Second:
Here's the code:
$(document).keyup(function(e) {
if (e.keyCode == 27) { // escape key maps to keycode `27`
$('#registerExist').prop('disabled', true);
$('#edit').prop('disabled', true);
document.getElementById("enStudID").value = "";
document.getElementById("enInfoID").value = "";
document.getElementById("enCoffID").value = "";
document.getElementById("enYearID").value = "";
}
});
Now, what I want to do are those two codes above will merge in one function and it will call the function and trigger all those codes so when I edit the code it will be centralized.
Here's my final code:
function enableRegButton() {
$('#registerExist').prop('disabled', false);
$('#edit').prop('disabled', false);
// regButton execute when Enter key pressed
$(document).unbind("keyup").keyup(function(e){
var code = e.which; // recommended to use e.which, it's normalized across browsers
settings();
});
}
// This code is for ESC button when pressed.
$(document).keyup(function(e) {
settings();
});
function settings(){
if(code==13)
{
$("#registerExist").click();
}
else if (code==27){ // escape key maps to keycode `27`
$('#registerExist').prop('disabled', true);
$('#edit').prop('disabled', true);
document.getElementById("enStudID").value = "";
document.getElementById("enInfoID").value = "";
document.getElementById("enCoffID").value = "";
document.getElementById("enYearID").value = "";
}
}
Problem:
You need to pass the key code to the settings
method.
$(document).keyup(function(e) {
settings(e.keyCode);
});
function settings(code) {
Use the browser's developer console when debugging Javascript issues, it's an invaluable tool and picks up problems like this quite easily.
您是在unbind回调中而不是在bind回调中分配code
变量:)
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.