iView input 输入时添加下拉框提示信息

需求

添加注解时需要根据 Input 输入值下拉显示提示信息,并可选

步骤

1.为目标 Input 绑定 on-focuson-blur 事件,聚焦时显示,失焦时隐藏;下拉提示信息使用 ul,li 标签显示,浮动样式使用 position 属性实现(设置为 Input 的 position 为 relative,ul 标签的 position 为 absolute);
2. 使用 Vue computed 属性,根据所需逻辑,实时计算所需要显示的列表数据(此方法不支持调接口获取数据,如需按输入调用接口,可在 Input on-focus 方法中调用接口);
3. ul 标签使用 v-show 判断当前是否应显示列表(showAnnotationTip && getAnnotationKeyList().length > 0),使用 @mousedown 方法监听鼠标选择并赋值。此外,需要为已选择的 key 设置高亮样式。

代码

html 部分

<Form 
	@submit.native.prevent 
	ref="textForm" 
	:model="formData"
>
	...
	<Form-item>
		<Input 
			v-model.trim="formData.keyName"
			placeholder="请输入"
			@on-focus="showTip()"
			@on-blur="hiddenTip()"
			style="width: 95%; position: relative"
		/> 
		<ul
			class="ul-wrapper"
			v-show="showAnnotationTip && getAnnotationKeyList().length > 0"
		>
			<li
				v-for="annotationKey in getAnnotationKeyList()"
				:key="annotationKey"
				:style="formData.keyName === annotationKey ? 'color: rgba(59,124,255,1);cursor: pointer; z-index: 200': 'cursor: pointer; z-index: 200'"
				@mousedown="selectedKey(annotationKey)"
			>
				{{ annotationKey }}
			</li>
		</ul>
	</Form-item>
</Form >

js 部分

export default {
	data() {
		return {
			showAnnotationTip: false,
			formData: {
				keyName: ''
			}
		}
	},
	computed: {
		providerOptions(){
			// 根据所需逻辑计算当前需要显示的提示信息
			return ['1111', '2222', '3333'];
		}
	},
	methods: {
		showTip(){
			this.showAnnotationTip = true;
		},
		hiddenTip(){
			this.showAnnotationTip = false;
		},
		getAnnotationKeyList() {
			if(!this.formData.keyName){
				return this.providerOptions;
			}
			//根据当前输入内容筛选显示数据
			return this.providerOptions.filter(item => item.indexOf(this.formData.keyName) !== -1);
		},
		selectedKey(annotationKey) {
			this.formData.keyName = annotationKey;
			this.showAnnotationTip = false;
		}
	}
}

css 部分

.ul-wrapper {
	position: absolute;
	margin: 5px;
	padding: 8px;
	min-width: 210px;
	border-radius: 4px;
	background-color: #fff;
	list-style: none;
	z-inde: 100;
	max-height: 232px;
	overflow-y: auto;
	box-sizing: border-box;
}

END

### 解决方案 为了防止 `iView` 输入框中的历史数据被浏览器自动填充,在 Vue 项目中可以采用多种策略来实现这一目标。 #### 方法一:调整隐藏 Input 的位置 对于文本输入框,应将隐藏的 `<input>` 放置在其下方;而对于密码输入框,则需将其放置于上方。这种布局能够有效阻止默认行为的发生[^1]: ```html <template> <div> <!-- 密码输入框 --> <span style="position: absolute; visibility: hidden;"> <input type="password" name="fakepwd"> </span> <Form :model="formInline" inline> <FormItem prop="passwd"> <Input type="password" v-model="formInline.passwd"></Input> </FormItem> </Form> <!-- 用户名输入框 --> <span style="position: absolute; visibility: hidden;"> <input type="text" name="fakeusernm"/> </span> <Form :model="formInline" inline> <FormItem prop="userName"> <Input type="text" v-model="formInline.userName"></Input> </FormItem> </Form> </div> </template> ``` #### 方法二:应用特殊样式 通过给定特定样式的隐藏域,可以使这些字段脱离可见区域而不影响实际使用的表单控件的功能[^4]: ```css <style scoped> .hidden-input { position: fixed; top: -1000px; opacity: 0; } </style> ``` 并将此类应用于额外创建的不可见输入元素上: ```html <input class="hidden-input" /> ``` #### 方法三:设置 Autocomplete 属性 尽管直接修改 `autocomplete` 属性可能不会总是奏效,但在某些情况下仍然值得尝试。可以在父级标签或者具体的 `<Input>` 组件内部指定该属性为 `"off"` 或者其他非标准值以试图绕过浏览器的记忆机制[^2]。 ```html <Form autocomplete="new-password">...</Form> <!-- or --> <Input type="text" autocomplete="new-username"></Input> ``` 以上三种方式可以根据具体需求组合使用,从而达到最佳的效果。值得注意的是,确保已正确加载了必要的 iView 资源文件也是解决问题的关键之一[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值