简体   繁体   中英

Javascript event keypress

A form has a input field which would contain text values . There is a Save button as well for the form .

Requrement: The save button should get disabled as soon as Input field goes empty.

Problem: Javascript events like change,blur ,focus etc require loss of focus(that is a click outside) ,Hence save button would not get disabled simultaneously the input goes blank without user clicking outside of the field.

The closest being 'keypress' event .However the problem with this is,as soon as I try to make the field empty it checks for the input value and since it already has some value the condition check I have put to disalbe the button fails.

Refer the code below I've used STUDY_NAME is the input field id SAVE_DRAFT_TAB_GENERAL is the save button id.

Code :

    if($(STUDY_NAME)){
        $(STUDY_NAME).addEvent("keypress", function(){
            if($(STUDY_NAME).value == ''){
                $(SAVE_DRAFT_TAB_GENERAL).disabled = true;
            }else {
                $(SAVE_DRAFT_TAB_GENERAL).disabled = false;
            }
        });
    }

您可以尝试使用在用户键入他的值后发生的 KeyUp 事件

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