开发者

Input text form, using javascript to clear and restore form contents problem

开发者 https://www.devze.com 2023-03-11 07:34 出处:网络
I have this line of HTML <input type=\"text\" name=\"addQty\" size=\"1\" class=\"addQty\" value=\"0\"

I have this line of HTML

<input type="text" name="addQty" size="1" 
       class="addQty" value="0" 
       onclick="$(this).val('')" 
       onblur="itmQtyChk($(this).val())" />

the itmQtyChk function does this:

function itmQtyChk( qty ) {
    if( qty == "") {
        $(this).val("0");
    } else {
        $(this).val(qty);
    }
 }

My problem is I want it to return the original value to the input text if they exit the field and don't change anything开发者_StackOverflow中文版, but it is not working.

Thank you for any help.


this in itmQtyChk function is not referring to the input but to the window object.

Change the function to accept the input as parameter:

function itmQtyChk(input) {
    if (input.val() == "") {
        input.val("0");
    }
    // the else part is not needed
}

with the onblur event also:

onblur="itmQtyChk($(this))"


Check this fiddle, it has lots of room for improvement, but it can help you:

http://jsfiddle.net/eDuKr/1/

$(function(){

    var cacheQty;

    $('.addQty').click(function(){
        cacheQty = $(this).val();
        $(this).val('');

    }).blur(function(){
        if ($(this).val() === ''){
            $(this).val(cacheQty);
        }else{
            cacheQty = $(this).val();
        }
    });   
});
0

精彩评论

暂无评论...
验证码 换一张
取 消