vue项目中input框不允许输入 必须通过选项选择的几种方式

vue项目中input框不允许输入 必须通过选项选择的几种方式

1: readonly属性

定义和用法

readonly 属性规定输入字段为只读。

只读字段是不能修改的。不过,用户仍然可以使用 tab 键切换到该字段,还可以选中或拷贝其文本。

readonly 属性可以防止用户对值进行修改,直到满足某些条件为止(比如选中了一个复选框)。然后,需要使用 JavaScript 消除 readonly 值,将输入字段切换到可编辑状态。

readonly 属性可与 或 配合使用。

<input type="text"  readonly>

2:disabled禁用

2:disabled 被禁用的 input 元素可复制,不能接收焦点,设置后文字的颜色会变成灰色。无法与 一起使用。

<el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" disabled @click.native="hFocus" />
使用b-bind单向绑定
<el-input :value="formData.departmentName" style="width:50%" placeholder="请选择部门" disabled @click.native="hFocus" />

readonly和disabled区别

区别:
(1) 表面上可看到的区别就是当这两个词都设置为true时,都为禁用状态,当鼠标移上时使用disable的相关控件时鼠标出现禁用样式,并且不可做任何操作,而Readonly还可以获取文本框里的焦点。
(2) Disable比readonly的使用范围比广,适用文本框、文本域、下拉框、button按钮、单选框…….而readonly只适用于input(text、passwork、textarea)。
(3) Disable设置为true之后是不可以向后台提交数据的,此时可以选择改用readonly进行禁用,或者在提交数据时取消禁用。

3:通过控制input的max length为0实现

<input type="text" maxlength="0">

4:失去焦点

4:οnfοcus="this.blur();“onfocuse是聚焦的意思,当你把光标放在文本框上输入的时候,就是聚焦,但这里添加了"this.blur()”,blur的作用就是去除聚焦,也就是你不能把光标放在这个文本框上,换句话说就是你不能输入文本了

<input type="text" value="hello JavaScript" onfocus="this.blur();">
好的,以下是一个使用 Vue 3 编写的代码,包含一个复选列表和一个按钮,点击按钮后会随机生成一个由选的食材组成的食物名称: ```html <!DOCTYPE html> <html> <head> <title>随机生成食物</title> <meta charset="UTF-8"> <script src="https://unpkg.com/vue@next"></script> <script> const app = Vue.createApp({ data() { return { ingredients: [ { name: "西红柿", checked: false }, { name: "鸡蛋", checked: false }, { name: "米饭", checked: false }, { name: "葱", checked: false }, { name: "面", checked: false } ], foodList: { "tomato-egg-rice": ["西红柿炒鸡蛋盖饭", ["西红柿", "鸡蛋", "米饭"]], "scallion-noodles": ["葱油拌面", ["葱", "面"]], "omelette-rice": ["蛋包饭", ["鸡蛋", "米饭"]], "tomato-rice": ["西红柿炒饭", ["西红柿", "米饭"]] }, foodResult: "" }; }, methods: { generateFood() { const ingredients = this.ingredients.filter(ingredient => ingredient.checked); const foodKeys = Object.keys(this.foodList); const randomFoodKey = foodKeys[Math.floor(Math.random() * foodKeys.length)]; const randomFood = this.foodList[randomFoodKey]; const ingredientsText = randomFood[1].filter(ingredient => { return ingredients.some(selectedIngredient => selectedIngredient.name === ingredient); }).join(", "); if (ingredientsText.length > 0) { this.foodResult = "使用了" + ingredientsText + "等食材的" + randomFood[0]; } else { this.foodResult = randomFood[0]; } } } }); </script> </head> <body> <div id="app"> <h1>随机生成食物</h1> <p>请选至少一种食材:</p> <form> <label v-for="(ingredient, index) in ingredients" :key="index"> <input type="checkbox" v-model="ingredient.checked">{{ ingredient.name }} </label><br> <br> <button type="button" @click="generateFood">生成食物</button> </form> <br> <p>{{ foodResult }}</p> </div> </body> </html> ``` 你可以将此代码复制到一个 HTML 文件,并在浏览器打开,即可看到一个使用 Vue 3 实现的界面。每次点击 “生成食物” 按钮时,代码将会随机选择一个食物,并将其名称显示在页面,同时会根据选的食材生成食物的说明。例如,如果选了西红柿和米饭,可能会生成类似于“使用了西红柿、米饭等食材的西红柿炒饭”这样的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值