我有一个带有特殊符号按钮的输入作为插件。
HTML
<div class="col-md-3 col-xs-9 col-sm-9">
<div class="input-group">
<input type="text" maxlength="10" class="form-control" id="symboltext"> <span class="input-group-btn">
<button class="btn btn-default" type="button" id="popbutton" data-placement="bottom" data-container="body"><span class="glyphicon glyphicon-edit"></span>
</button>
</span>
</div>
</div>
JQuery的
var popupElement = '<div class="btn-group" role="group"><button class="btn btn-sm btn-default" id="regbtn"><span class="glyphicon glyphicon-registration-mark"></button><button class="btn btn-sm btn-default" id="copyrightbtn"><span class="glyphicon glyphicon-copyright-mark"><span></button><button class="btn btn-sm btn-default" id="trademarkbtn"><sup>TM</sup></button></div>';
var lastPoint = 0;
$('#popbutton').popover({
animation: true,
content: popupElement,
html: true
});
function textbox() {
var ctl = document.getElementById('symboltext');
var startPos = ctl.selectionStart;
var endPos = ctl.selectionEnd;
lastPoint = startPos;
return {
s: startPos,
e: endPos
};
}
function getVal(insert) {
var val = $("#symboltext").val();
var index = textbox();
val = val.slice(0, index.s) + insert + val.slice(index.s)
lastPoint += 1;
return val;
}
$(document).on('click', "#trademarkbtn", function() {
var val = getVal("\u2122");
var input = $("#symboltext");
input.val(val).focus();
input[0].selectionStart = input[0].selectionEnd = lastPoint;
});
$(document).on('click', "#regbtn", function() {
var val = getVal("\u00AE");
var input = $("#symboltext");
input.val(val).focus();
input[0].selectionStart = input[0].selectionEnd = lastPoint;
});
$(document).on('click', "#copyrightbtn", function() {
var val = getVal("\u00A9");
var input = $("#symboltext");
input.val(val).focus();
input[0].selectionStart = input[0].selectionEnd = lastPoint;
});
我已将输入的最大长度设为10.但在到达之后我仍然可以使用附加/特殊符号按钮输入字符。
答案 0 :(得分:1)
使用此
<input id="field" type="text" maxlength="10" />
Jquery的
$('#field').keypress(function (event) {
if ($('#field').val().length <= 10) {
if (event.keyCode != '110' && event.charCode != '46') {
$('#field').attr('maxlength', '09');
} else if (event.keyCode == '110' || event.charCode == '46') {
$('#field').attr('maxlength', '11');
}
}
});