If input value is blank, assign a value of “empty” with Javascript

后端 未结 6 1366
庸人自扰
庸人自扰 2021-02-01 21:49

So I have an input field, if it\'s blank, I want its value to be the words \"empty\", but if there is any value inputted, I want the value to be the inputted value. I want to us

6条回答
  •  野的像风
    2021-02-01 22:53

    I'm guessing this is what you want...

    When the form is submitted, check if the value is empty and if so, send a value = empty.

    If so, you could do the following with jQuery.

    $('form').submit(function(){
        var input = $('#test').val();
        if(input == ''){
             $('#test').val('empty');
        }    
    });
    

    HTML

    http://jsfiddle.net/jasongennaro/NS6Ca/

    Click your cursor in the box and then hit enter to see the form submit the value.

提交回复
热议问题