关于<lable>标签的提交

本文详细介绍了如何在HTML表单中正确使用&lt;lable&gt;标签,由于该标签无法直接提交,文章提供了通过给&lt;lable&gt;标签设置ID并结合&lt;hidden&gt;标签来间接提交其值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<lable>标签在表单中是不能被直接提交的,也就是说即使你给它一个name,你也取不到它的值。

如果你想提交<lable>标签的值,需要你给它一个id,然后通过document.getElementById("ID名").innerText的方式获取它的值,然后通过<hidden>的形式传值,这样就可以做到了。

业务画面:<template> <my-form ref="forRef" :model="data"> <my-form-item label="产品名称" prop="name"> <my-input v-model="data.name" :rules="[ { required: true, message: '请输入产品名称', trigger: 'blur' }, { max: 10, maxMessage: '产品名称不能超过10个汉字' } ]" /> </my-form-item> <my-form-item label="产品描述" prop="age"> <my-input v-model="data.age" :rules="[ { required: true, message: '请输入产品描述', trigger: 'blur' }, { max: 10, maxMessage: '姓名不能超过200个汉字' } ]" /> </my-form-item> <my-form-item label="性别" prop="sex" :rules="[ { required: true, message: '请选择性别', trigger: 'blur' },]"> <my-select v-model="data.sex"> <my-option v-for="item in sexOptions" :label="item.lable" :value="item.value" /> </my-select> </my-form-item> <my-form-item label="产品单价" prop="price"> <my-input v-model="data.price" :rules="[ { required: true, message: '请输入产品单价', trigger: 'blur' }, { min: 0, message: '产品单价不能小于0', trigger: 'blur' }, { max: 10000, message: '产品单价不能超过10000', trigger: 'blur' } ]" /> </my-form-item> <my-form-item label="产品库存" prop="price"> <my-input v-model="data.price" :rules="[ { required: true, message: '请输入产品库存', trigger: 'blur' }, { min: 0, message: '产品库存不能小于0', trigger: 'blur' }, { max: 10000, message: '产品库存不能超过10000', trigger: 'blur' } ]" /> </my-form-item> </my-form> {{ data }} <button @click="checkForm">提交</button> <button @click="clearForm">清空</button> </template> <script setup> import myForm from '@/components/myForm'; import myFormItem from '@/components/myForm/myFormItem'; import MyInput from '@/components/input' import MySelect from '@/components/mySelect' import MyOption from '@/components/mySelect/myOption' const forRef = ref() const sexOptions = reactive([{ lable: "男", value: 1 }, { lable: "女", value: 2 }]) const data = reactive({ name: "", age: "", sex: "", price: "", formCheck: null }) function checkForm() { data.formCheck = forRef.value.validate() } function clearForm() { forRef.value.resetField() } const rules = reactive({ name: [ { required: true, message: '请输入产品名称', trigger: 'blur' }, { max: 10, message: '长度超过10位', trigger: 'blur' }, ], description: [ { required: true, message: '请输入产品描述', trigger: 'blur' }, { max: 200, message: '长度超过200位', trigger: 'blur' }, ], category: [ { required: true, message: '请选择产品分类', trigger: 'change' }, ], }); </script> form画面:<template> <slot> </slot> </template> <script setup> import { provide } from 'vue'; const props = defineProps({ model: { type: Object, required: true } }) const form = reactive({ childItem: [], childProp: [] }) provide('addChildItem', (item) => { form.childItem.push(item) }) provide('addChildProp', (item) => { form.childProp.push(item) }) function validate() { let bool = true form.childItem.forEach(item => { let check = item.validate() if (check) { bool = false } }) return bool } function resetField() { form.childProp.forEach(item => { if (props.model[item]) { props.model[item] = null } }) } defineExpose({ validate, resetField }) </script> form-item画面:<template> <div style="display: flex; padding: 5px; gap: 5px;"> <label class="label">{{ props.label }}</label> <label>:</label> <slot></slot> </div> </template> <script setup> const props = defineProps({ label: { type: String, required: true, default: '' }, prop: { type: String, default: '' } }) const addChildProp = inject('addChildProp') onMounted(() => { if (addChildProp) { addChildProp(props.prop) } }) </script> <style> .label { min-width: 100px; text-align: right; } </style> select组件:<template> <div class="select"> <button class="trigger" @click="toggleMenu"> {{ selectedLabel || placeholder }} <span class="arrow" :class="{ 'rotated': isOpen }">▼</span> </button> <div class="menu" :class="{ 'menu_open': isOpen }"> <template v-for="item in selectOptions"> <div class="option" @click="handleClick(item)" :class="{ 'selected': item.selected }"> <span>{{ item.label }}</span> <!-- <span class="left-content">{{ item.label }}</span> --> </div> </template> </div> </div> <slot></slot> </template> <script setup> const model = defineModel() const selectedLabel = ref('') const selectOptions = ref([]) const isOpen = ref(false) const props = defineProps({ placeholder: { type: String, default: '请选择' }, options: { type: Array, default: [] } }) const emit = defineEmits(['change']) function handleClick(item) { model.value = item.value toggleMenu() } function setOtherOptionDefault(value) { selectOptions.value.forEach(item => { if (item.value != value) { item.selected = false } else { item.selected = true } }) } function toggleMenu() { isOpen.value = !isOpen.value } provide('pushOptions', item => { selectOptions.value.push(item) }) watch( () => model, // 监听的属性 (newVal, oldVal) => { // 变化时的回调函数 if (newVal != null && newVal != undefined && newVal.value != null && newVal.value != undefined) { selectOptions.value.forEach(item => { if (item.value == newVal.value) { selectedLabel.value = item.label setOtherOptionDefault(newVal.value) } }) } else { selectedLabel.value = "" } }, { deep: true } ); onMounted(() => { if (props.options.length > 0) { selectOptions.value = props.options } selectOptions.value.forEach(item => { if (item.value == model.value) { selectedLabel.value = item.label setOtherOptionDefault(model.value) } }) }) </script> <style scoped lang="scss"> .option { width: 100%; padding: 3px 0px 3px 0px; &:hover { background-color: #fa0808; } span { margin-left: 15px; font-size: 14px; user-select: none; } } .selected { color: #409eff; } .select { position: relative; width: 200px; } .trigger { width: 100%; padding: 8px 12px; border: 1px solid #ccc; background-color: #fff; text-align: left; cursor: pointer; } .arrow { float: right; transition: transform 0.3s ease; } .arrow.rotated { transform: rotate(180deg); } .menu { position: absolute; top: 100%; left: 0; width: calc(100% - 1px); height: 0px; overflow-x: auto !important; overflow-y: auto !important; border: 1px solid #ccc; border-top: none; background: white; z-index: 10; transition: height 0.3s ease; &::-webkit-scrollbar { width: 5px; /* 垂直滚动条宽度 */ height: 5px; /* 水平滚动条高度(可选) */ } /* 滚动条滑块部分 */ &::-webkit-scrollbar-thumb { background-color: rgba(0, 0, 0, 0.3); border-radius: 4px; } /* 滚动条轨道 */ &::-webkit-scrollbar-track { background-color: #f1f1f1; } } .menu.menu_open { height: 100px !important; } .fade-enter-active, .fade-leave-active { transition: opacity 0.2s; } .fade-enter-from, .fade-leave-to { opacity: 0; } </style> 子组件:<template> </template> <script setup> const props = defineProps({ label: { type: String, required: true }, value: { type: [String, Number], required: true }, }) const pushOptions = inject('pushOptions') onMounted(() => { pushOptions && pushOptions({ value: props.value, label: props.label, selected: false },) }) </script> 我想实现下拉框的check
07-17
<template> <!-- 报警自动转工单预案 --> <div class="alarm-work-order"> <template v-if="companyInfo.companyCode"> <a-spin :spinning="isLoading" :delay="500" :tip="$t('加载中...')"> <div class="edit-view"> <div class="input-item"> <div class="input-lable text-bold"> <span>{{ $t('预案是否启用') }}</span> </div> <div class="input-content"> <a-radio-group v-model="formData.enabled" :default-value="1"> <a-radio :value="1">{{ $t('启用') }}</a-radio> <a-radio :value="0">{{ $t('禁用') }}</a-radio> </a-radio-group> </div> </div> <div class="input-item"> <div class="input-lable text-align"> <span> {{ $t('报警类型:') }}</span> </div> <div class="input-content"> <a-select collapseTags mode="multiple" :maxTagCount="2" v-model="formData.type" allowClear placeholder="请选择" optionFilterProp="label" > <a-select-option v-for="(item, index) in typeOptions" :value="item.value" :key="index"> {{ item.label }} </a-select-option> </a-select> </div> </div> <div class="input-item"> <div class="input-lable"> <span> {{ $t('工单派发配置:') }}</span> </div> <div class="table-box" id="tableBox"> <a-table rowKey="department" :columns="columns" :dataSource="tableList" :scroll="{ x: '100%' }" :pagination="false" > <template slot="staff" slot-scope="text, record, index"> <a-select v-model="record.staffConfig" mode="multiple" placeholder="选择人员" optionFilterProp="label" :maxTagCount="2" allowClear @change="val => handleStaffChange(val, 'staffConfig', index)" > <a-select-option v-for="person in staffList" :key="person.id" :label="person.name" :value="person.id" :disabled="person.disabled" > {{ person.name }} </a-select-option> </a-select> </template> <template slot="time" slot-scope="text, record, index"> <a-select v-model="record.timeConfig" @change="val => changeTime(val, 'timeConfig', index)" placeholder="选择期望完成时间" > <a-select-option v-for="option in timeOptions" :key="option.value" :value="option.value"> {{ option.label }} </a-select-option> </a-select> </template> </a-table> </div> </div> <div class="btn-view action-list"> <a-button @click="onSave" type="primary">{{ $t('确定') }}</a-button> <a-button @click="onCancel" type="normal">{{ $t('取消') }}</a-button> </div> </div> </a-spin> </template> </div> </template> <script> export default { props: { orgInfo: { type: Object, default: () => { return {}; }, }, companyInfo: { type: Object, default: () => { return {}; }, }, }, data() { return { isLoading: false, formData: {}, typeOptions: [ { label: '选项1', value: 1, }, { label: '选项2', value: 2, }, { label: '选项3', value: 3, }, { label: '选项4', value: 4, }, ], columns: [ { title: '部门', key: 'department', dataIndex: 'department', ellipsis: true, }, { title: '用户', dataIndex: 'staff', scopedSlots: { customRender: 'staff' }, key: 'staff', }, { title: '期望完成时间设定', dataIndex: 'time', scopedSlots: { customRender: 'time' }, key: 'time', }, ], tableList: [ { department: '技术部', staffConfig: ['001', '002'], timeConfig: '24h', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, { department: '技术部', staffConfig: [], timeConfig: '7d', }, ], timeOptions: [ { label: '报警后12小时', value: '12h' }, { label: '报警后24小时', value: '24h' }, { label: '报警后48小时', value: '48h' }, { label: '报警后7日内', value: '7d' }, ], staffList: [], checkedUser: [], }; }, mounted() { this.staffList = [ { id: '001', name: '张三' }, { id: '002', name: '李四' }, { id: '003', name: '王五' }, { id: '004', name: '赵六' }, ]; this.updateStatus(); }, methods: { changeTime(value, key, index) { this.updateData(value, key, index); }, handleStaffChange(value, key, index) { // 人员只能选择两位 if (value && value.length > 2) { this.$message.destroy(); return this.$message.warning('人员最多选择2位'); } this.updateData(value, key, index); this.updateStatus(); }, updateStatus() { const selectedIds = new Set(); this.tableList.forEach(row => { if (Array.isArray(row.staffConfig)) { row.staffConfig.forEach(id => selectedIds.add(id)); } }); this.staffList.forEach((person, index) => { const shouldDisable = selectedIds.has(person.id); if (person.disabled !== shouldDisable) { this.$set(this.staffList, index, { ...person, disabled: shouldDisable, }); } }); }, updateData(value, key, index) { const target = this.tableList[index]; if (target) this.$set(target, key, value); }, // 确定 onSave() {}, // 取消 onCancel() {}, }, }; </script> <style lang="less" scoped> .alarm-work-order { height: 100%; width: 100%; position: relative; box-sizing: border-box; .edit-view { width: 100%; padding: 46px 32px 24px 32px; .input-item { width: 100%; display: flex; margin-bottom: 22px; &:last-child { margin-bottom: 0 !important; } } .input-lable { width: 88px; min-width: 88px; justify-content: start; display: flex; color: #808e9d; font-size: 14px; line-height: 19px; margin-right: 24px; span { white-space: nowrap; } } .text-bold { font-weight: bold; color: #4e5c6b; } .text-align { align-items: center; } .btn-view { display: flex; align-items: center; justify-content: flex-start; padding-left: 112px; margin-top: 24px; & > button { margin-right: 12px; } } } } </style> 代码评审
07-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值