简体   繁体   中英

jquery mask masked doesnt work. How set initial value to mask

I try to set initial value with jQuery Mask but the docs doesnt say correct way to it. I tried the next steps

    const options = {
        onKeyPress: (cep, e) => this.onChangeHandler(e),
        default: initialValue
    }
    $('#id').mask(maskPattern, options);

also

$('#id').mask(maskPattern, options);
$('#id').masked(initialValue);

But neither ways dont work.

I found a work around $('#id').val(initialValue).trigger('input'); at github issues and im going to share it in the answers, but i'm looking for the genuine way

masked() - Gets a masked value programmatically

You can set the value by

$('#id').val(initialValue).mask(maskPattern, options);

OR

$('#id').mask(maskPattern, options);
$('#id').val(initialValue).trigger('input');

More info

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