这是我的代码。参见<v-text-field>
标签。在keypress
事件上,如果文本框已满,它将截取输入值并将其转换为其他值(在光标处插入按键字符,并将其余字符推入后续输入框)。
当我模糊(聚焦)输入时,输入会在第一个BUT处以正确的值更新,它会返回到keypress
事件之前的值。另外,溢出字符已正确地放在后续输入框中,但其值不会保持不变-会更改焦点。
我还没有编写任何可以执行这些操作的事件处理程序。我尝试删除<v-form>
和:rules
属性。
我还尝试将每个<v-text-field>
绑定到v-model
并将新的输入值分配给模型(2级数组),当我在Vue Devtools中检查它时,我注意到它不会更新直到我进行模糊/聚焦。更新的值似乎也很奇怪。
我已经尝试在设置输入值后触发change
,甚至keyup
,但无济于事。
我已经尝试过使用非JQuery方式设置输入值,没有区别。
event.preventDefault()
处理程序中有无keypress
的尝试,没有区别。
尝试使用keyup
而不是keypress
只是为了查看行为(我需要keyup
),但同样的问题。
**更新:也尝试删除所有v-model
,没有区别。
Vuetify版本:1.5.17。
可能是什么原因造成的?请帮忙!我保证会在我有答辩权时将其付款并在此帮助其他人!每个小提示都会有很大帮助!
<v-stepper
v-model="stepperStep"
>
<v-stepper-items>
<v-form autocomplete="off"
ref="form"
v-model="valid"
lazy-validation >
<v-stepper-content
v-for="n in numLines"
:key="`${n}-content`"
:step="n"
>
<div v-show="false" :id="`wordLine_`+(n-1)" :data-numWords="wordLines[n-1].length" class="wordLine" style="display:flex; justify-content:center; margin:0 0 25px;">
<div v-for="(wordSize, key2) in wordLines[n-1]">
<div class="wordBox" style="display:flex; color:#f00;">
<v-text-field required class="wordInput" data-isWordInput="1" :stepperStep="n" :rel="key2" :rules="requiredRule" outline single-line dense height="36" @click:clear="wordInputCleared(n-1, key2)" :size="wordSize" :maxlength="wordSize"></v-text-field>
</div>
</div>
</div>
</v-stepper-content>
</v-form>
</v-stepper-items>
<v-stepper-header v-show="numLines > 1">
<template v-for="n in numLines">
<v-stepper-step
:key="`${n}-step`"
:complete="stepperStep == n"
:step="n"
:editable="true"
>
</v-stepper-step>
</template>
</v-stepper-header>
</v-stepper>
在mounted()
下:
$(document).on('keypress', '.wordInput input[type="text"]', function(event) {
if (event.keyCode == 32) { event.preventDefault(); return; }
if (event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 9 || event.keyCode == 16) return; // arrow keys, tab, shift
let sizeRemaining = $(this).attr('maxlength') - $(this).val().length;
let selectedText = $(this)[0].value.slice($(this)[0].selectionStart, $(this)[0].selectionEnd);
if (selectedText.length > 0) return; // if text is selected, we want this triggered keystroke to replace the selection (default behavior), not auto-advance
let cursorAtLastPosition = ($(this)[0].selectionEnd == $(this).attr('maxlength'));
if (event.keyCode != 8 && event.keyCode != 46) {
if (sizeRemaining == 0) {
event.preventDefault();
let keyVal = event.key;
let lastCharBeforeDisplaced = $(this).val().charAt($(this).val().length-1);
if (cursorAtLastPosition) {
if (parseInt($(this).attr('rel'))+1 == $(this).closest('.wordLine').attr('data-numWords')) { // last wordbox
if (_this.stepperStep == _this.numLines) { // last/only line
$('#btnFinished').focus(); return;
}
} else {
let $nextWord = $(this).closest('.wordLine').find('.wordInput').find('input[type="text"][rel="'+(parseInt($(this).attr('rel'))+1)+'"]');
lastCharBeforeDisplaced = keyVal;
}
} else { // insert keyVal at cursor
let lastCharRemoved = $(this).val().slice(0, -1);
$(this).val(lastCharRemoved.slice(0, $(this)[0].selectionEnd) + keyVal + lastCharRemoved.slice($(this)[0].selectionEnd));
}
_this.cascadeInsertNextWordbox($(this), lastCharBeforeDisplaced);
}
}
});
在methods
下:
cascadeInsertNextWordbox($currentWordbox, displacedChar) {
let $nextWordbox = this.getNextWordbox($currentWordbox);
if ($nextWordbox.length) { // exists
if ($nextWordbox.val().length == $nextWordbox.attr('maxlength')) { // is full
let lastCharBeforeDisplaced = $nextWordbox.val().charAt($nextWordbox.val().length-1);
$nextWordbox.val($currentWordbox.val().slice(0, -1)); // remove last char
this.cascadeInsertNextWordbox($nextWordbox, lastCharBeforeDisplaced);
}
}
$nextWordbox.val(displacedChar + $nextWordbox.val());
},