1.html
<input type="text" name="count" th:id="${item.treeId}"
onkeyup="buyMode(this.id,this.value)"
class="layui-input layui-input-num"
oninput="value=value.replace(/[^\d]/g,'')"/>
2.js
<script th:inline="javascript">
<!--试卷配置:文本框输入数字校验-->
function buyMode(inputId, inputValue) {
var arr = [];
if (statisticsKnowledgeDtos && statisticsKnowledgeDtos.length > 0) {
for (let i = 0; i < statisticsKnowledgeDtos.length; i++) {
const statisticsKnowledge = statisticsKnowledgeDtos[i];
arr.push(statisticsKnowledge.knowledgeId);
if (statisticsKnowledge.knowledgeId && statisticsKnowledge.knowledgeId == inputId) {
if (inputValue > statisticsKnowledge.questionNum) {
layer.msg("当前输入的题数大于备选题数,请重新输入。", {icon: 2, time: 2000});
$("#" + inputId).val("");
return false;
}
}
}
if (arr.indexOf(inputId) == -1) {
layer.msg("当前没有备选题,请重新输入。", {icon: 2, time: 2000});
$("#" + inputId).val("")
}
}
}
</script>
3.页面展示
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f9c0f99bd8cf473ec86d55ab8ddfd44c.jpeg)