[#0x0053] 阻止 form submit 的方法

有两个地方可以可以阻止form submit,一是<form οnsubmit="">,而是<form>下的<input type="submit" οnclick="">,只要这两个函数有一个是return false;,那么点击这个button并不会submit form。

 

具体的执行流程是这样的:

if (submitInput.onclick() == false) {
	// 不触发 form.onsubmit();
	// form 不提交;
} else {
	// 触发 form.onsubmit();
	if (form.onsubmit() == false) {
		// form 不提交
	} else {
		// 提交 form
	}
}

 

所以在这个两个函数里面可以加一些验证工作,同时,可以根据验证结果来判断是否提交form。

比如有:

var checkForm = function() {
	// 获取输入
	if (校验失败)
		return false;
	else
		return true;
}

 

然后把这个函数写到onsubmit或是onclick,也可以针对不同的button写不同的逻辑的校验函数,比如这样:

<html>
	<form οnsubmit="return checkFormStep2();" method="post" action="/XXX.do">
		<input type="text" id="username">
		<input type="password" id="password">

		<input type="submit" οnclick="return checkFormStep1();" value="go" />
	</form>
</html>

在点击时先执行checkFormStep1(),在form提交前再执行checkFormStep2(),两个都通过都return true了才提交

 

也可以针对“撤销”功能的按钮写一些类似清空输入的功能,再禁止它提交form,比如这样:

<html>
	<form method="post" action="/XXX.do">
		<input type="text" id="username">
		<input type="password" id="password">

		<input type="submit" οnclick="return checkFormStep();" value="go" />
		<input type="submit" οnclick="clearInput(); return false;" value="reset" />
	</form>
</html>

 

<!-- 新增帧弹窗 --> <el-dialog :title="isEditMode ? &#39;编辑帧&#39; : &#39;新增帧&#39;" v-model="addFrameDialogVisible" width="30%" @close="resetState" ><el-form :model="frameFrom" :rules="rules" ref="formRef" label-width="100px" > <!-- 帧编码 --> <el-form-item label="帧编码" prop="frameIdcode"> <el-input v-model="frameFrom.frameIdcode" placeholder="请输入16进制编码" ><template #prepend>0x</template></el-input > </el-form-item> <!-- 名称 --> 。。。 <!-- 描述 --> 。。。</el-form ><template #footer> <el-button @click="addFrameDialogVisible = false">取消</el-button> <el-button type="primary" @click="submitForm">确定</el-button> </template></el-dialog > // 验证规则 const rules = { frameIdcode: [ { required: true, message: &#39;帧编码不能为空&#39;, trigger: &#39;blur&#39; }, { validator: (_, value, callback) => { const hexRegex = /^[0-9A-Fa-f]+$/ if (!hexRegex.test(value)) { callback(new Error(&#39;必须是有效的16进制格式&#39;)) } else { callback() } }, trigger: &#39;blur&#39; }, { validator: (_, value, callback) => { // 检查数组内重复 const isDuplicateInArray = frameArray.value.some( (item) => item.frameIdcode === value ) // 检查protocol对象重复 const isDuplicateInProtocol = currentProtocol.value?.frames.some( (item) => { // 将数值转换为16进制字符串(小写形式) const hexCode = item.nframeIdcode.toString(16).toLowerCase() // 将比较值也转为小写形式进行匹配 return hexCode === value.toString().toLowerCase() } ) if (isDuplicateInArray || isDuplicateInProtocol) { callback(new Error(&#39;帧编码不能重复&#39;)) } else { callback() } }, trigger: [&#39;blur&#39;, &#39;change&#39;] } ], ... } //编辑帧基本数据 const frameEdit = () => { if (!selectedFrame.value) { ElMessage.warning(&#39;请先选择要编辑的帧&#39;) return } // 填充表单数据 frameFrom.value = { ...selectedFrame.value } // 查找编辑项的索引 editingIndex.value = frameArray.value.findIndex( (item) => item.frameIdcode === selectedFrameIdcode.value ) isEditMode.value = true addFrameDialogVisible.value = true }编辑帧时填充数据会帧编码不能重复无法通过验证
最新发布
04-03
<think>好的,我现在要解决用户的问题,即在使用Element UI的弹窗进行帧编辑时,填充数据会导致帧编码重复验证失败。用户提供的代码中,验证规则会检查当前输入的帧编码是否在现有数组或协议中存在重复。当编辑时,表单中的帧编码可能已经被填充,这时候验证器会检测到重复,但实际上这是同一个条目,应该允许保存。 首先,我需要理解验证逻辑。在用户提供的代码中,验证规则`frameIdcode`的第三个验证器检查两种情况:`frameArray`中的重复和`currentProtocol`中的重复。当编辑时,表单被填充了已有的帧编码,这时候验证器会认为该编码已经存在于数组中,导致验证失败。 问题出在验证器没有排除当前正在编辑的项。在编辑模式下,应该忽略当前项自身的重复检查。因此,解决方案是在检查重复时,排除当前正在编辑的项。例如,在`frameArray`中检查时,跳过当前编辑的索引;在`currentProtocol`中检查时,排除当前正在编辑的帧。 接下来,我需要考虑如何在验证器中获取当前编辑的项的标识。用户可能需要在验证规则中访问当前编辑的索引或帧的唯一标识,比如`frameIdcode`。可以通过在验证规则中使用闭包或者将编辑项的信息存储在可访问的位置,比如Vue的响应式数据中。 在用户提供的代码中,`editingIndex`和`selectedFrameIdcode`可能存储了当前编辑项的信息。因此,可以在验证器中使用这些值来排除当前项。例如,在检查`frameArray`时,如果存在`editingIndex`,则跳过该索引的项;在检查`currentProtocol`时,排除当前正在编辑的帧的`nframeIdcode`。 修改验证器逻辑:在检查重复时,如果是编辑模式,需要排除当前项。例如,在`frameArray`的检查中,使用`editingIndex.value`来过滤掉当前编辑的项;在`currentProtocol`的检查中,比较时排除当前帧的ID。 然后,需要确保在提交表单时,验证规则正确应用了这些排除条件。同时,需要处理`editingIndex`和`selectedFrameIdcode`的更新,确保在打开编辑弹窗时正确设置这些值。 最后,测试修改后的验证规则,确保在编辑时填充的数据不会触发重复错误,同时在新增时仍然正确检查重复。</think>要解决编辑时帧编码重复验证的问题,需要在校验规则中排除当前正在编辑的项。以下是修改后的验证规则和说明: ```javascript // 修改后的验证规则 const rules = { frameIdcode: [ { required: true, message: &#39;帧编码不能为空&#39;, trigger: &#39;blur&#39; }, { validator: (_, value, callback) => { const hexRegex = /^[0-9A-Fa-f]+$/ if (!hexRegex.test(value)) { callback(new Error(&#39;必须是有效的16进制格式&#39;)) } else { callback() } }, trigger: &#39;blur&#39; }, { validator: (_, value, callback) => { // 排除当前编辑项 const currentEditingCode = isEditMode.value ? selectedFrameIdcode.value?.toLowerCase() : null // 检查数组内重复(排除当前编辑项) const isDuplicateInArray = frameArray.value.some((item, index) => { return index !== editingIndex.value && item.frameIdcode === value }) // 检查protocol对象重复(排除当前编辑项) const isDuplicateInProtocol = currentProtocol.value?.frames?.some( (item) => { const hexCode = item.nframeIdcode.toString(16).toLowerCase() return hexCode !== currentEditingCode && hexCode === value.toString().toLowerCase() } ) if (isDuplicateInArray || isDuplicateInProtocol) { callback(new Error(&#39;帧编码不能重复&#39;)) } else { callback() } }, trigger: [&#39;blur&#39;, &#39;change&#39;] } ] } ``` **修改说明:** 1. 在数组重复检查中,通过`index !== editingIndex.value`排除当前正在编辑的项 2. 在协议对象检查中,通过`hexCode !== currentEditingCode`排除当前编辑项 3. 新增`currentEditingCode`变量获取当前正在编辑的原始编码(转换为小写) 4. `editingIndex`在编辑时会被设置为当前编辑项在数组中的索引位置 **需要同步修改的代码:** ```javascript const frameEdit = () => { if (!selectedFrame.value) { ElMessage.warning(&#39;请先选择要编辑的帧&#39;) return } // 记录原始编码(转换为小写) selectedFrameIdcode.value = selectedFrame.value.frameIdcode.toLowerCase() // 填充表单数据 frameFrom.value = { ...selectedFrame.value } // 查找编辑项的索引 editingIndex.value = frameArray.value.findIndex( (item) => item.frameIdcode.toLowerCase() === selectedFrameIdcode.value ) isEditMode.value = true addFrameDialogVisible.value = true } ``` **注意事项:** 1. 所有比较操作都统一转换为小写形式 2. `selectedFrameIdcode`需要保存原始值的副本 3. 在表单提交时,需要处理编码的大小写转换和格式验证
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值