简体   繁体   中英

Code won't work when merging a javascript code in one function

Good day everyone!

I have a problem merging the codes in one function. (If it's possible).

First:

  • There is a table.
  • When the table row click two buttons will be enabled.

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:

  • When Escape key pressed it will disable all bind buttons.

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:

  • Only the enable button is working when table row is click
  • Pressing Escape key will not disable the enabled buttons.
  • Code won't run when pressing Enter Key.

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.

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