这是我的代码。见 <v-text-field>
标签。上 keypress
事件,如果文本框已满,它会拦截输入值并将其转换为其他内容(在光标处插入按键字符并将其余字符推送到后续输入框中)。
输入首先用正确的值更新,但是当我模糊它(聚焦)时,它会回到 keypress
之前的值事件。此外,溢出字符在随后的输入框中被正确添加,但它的值不会保持不变——它会在焦点上发生变化。
我还没有编写任何可以执行这些操作的事件处理程序。我试过删除 <v-form>
和 :rules
属性。
我也试过把每个 <v-text-field>
绑在一起到 v-model
并将新的输入值分配给模型(2 级数组),当我在 Vue Devtools 中检查它时,我注意到它在我执行模糊/聚焦之前不会更新该值。更新后的值似乎也不稳定。
我试过触发 change
,甚至 keyup
,设置输入值后,但无济于事。
我试过设置输入值的非JQuery方式,没有区别。
试过有无 event.preventDefault()
内keypress
处理程序,没有区别。
试过 keyup
而不是 keypress
只是为了查看行为(我需要 keyup
)但是,同样的问题。
**更新:还尝试删除所有 v-model
s,没有区别。
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());
},
最佳答案
在 this link 找到解决方案! $forceUpdate() 是需要的。通过 v-model 更新:
<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 v-model="wordInputs[n-1][key2]" 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 v-show="false" class="wordInputSizeRemaining" style="margin:4px 0px 0px 6px;">{{wordSize}}</div>
</div>
</div>
</div>
和 :
} else { // insert keyVal at cursor
let lastCharRemoved = $(this).val().slice(0, -1);
_this.wordInputs[$(this).attr('stepperstep')-1][$(this).attr('rel')] = lastCharRemoved.slice(0, $(this)[0].selectionEnd) + keyVal + lastCharRemoved.slice($(this)[0].selectionEnd);
_this.$forceUpdate();
}
关于javascript - Vuetify 文本字段 : Changes To Values Don't Persist (Lost on Blur or Focus),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59753508/