0
ユーザはアルファ、スペース、カンマだけを入力できる[タイプ=テキスト]を入力しました。 "入力"の各カンマの後にスペースが必要です。 アイデアjQueryを使って例えばユーザが入力を入力したときのシンボルの後の空白スペース
ユーザはアルファ、スペース、カンマだけを入力できる[タイプ=テキスト]を入力しました。 "入力"の各カンマの後にスペースが必要です。 アイデアjQueryを使って例えばユーザが入力を入力したときのシンボルの後の空白スペース
:
$("#landing-search-input").keyup(function(event) {
if (event.keyCode == 188) {
$(this).val($(this).val() + " ");
}
});
または
$("#landing-search-input").keyup(function(event) {
var inputValue = $(this).val();
if (inputValue.substr(-1) == ",") {
$(this).val(inputValue + " ");
}
});
あなたは時にキャレットスペースが正しい場所に追加され
後の空白の後に...あなたはIE 6を含むすべての主要なブラウザで動作します次のようなものを、必要があります
コード:
function insertTextAtCursor(el, text) {
var val = el.value, endIndex, range;
if (typeof el.selectionStart != "undefined"
&& typeof el.selectionEnd != "undefined") {
endIndex = el.selectionEnd;
el.value = val.slice(0, endIndex) + text + val.slice(endIndex);
el.selectionStart = el.selectionEnd = endIndex + text.length;
} else if (typeof document.selection != "undefined"
&& typeof document.selection.createRange != "undefined") {
el.focus();
range = document.selection.createRange();
range.collapse(false);
range.text = text;
range.select();
}
}
document.getElementById("some_input").onkeypress = function(evt) {
evt = evt || window.event;
var charCode = typeof evt.which == "number" ? evt.which : evt.keyCode;
if (String.fromCharCode(charCode) == ",") {
insertTextAtCursor(this, ", ");
return false;
}
};
キャレットが入力テキストの最後にない場合、これは機能しません。 –