How can I add an unremovable prefix to an HTML input field?

前端 未结 16 1398
猫巷女王i
猫巷女王i 2020-12-13 04:23

Using jQuery, how can I add a default value of http:// into an input field that can’t be removed, but that still allows you to type a URL after it?

Defa

16条回答
  •  时光取名叫无心
    2020-12-13 05:04

    UPDATE: Guillaumesm's answer to Ecmascript 5:

    Form.prototype.set_HttpPrefix = function (e) {
        if (e.data.$url.val().length < 7) {
            e.data.$url.val('http://')
    
        }    
        else if (e.data.$url.val().indexOf('http://') !== 0) {
            e.data.$url.val('http://' + String.fromCharCode(e.which)) 
        }
    }
    

提交回复
热议问题