当前位置:   article > 正文

JavaScript上传文件名重复自动加(1)、文件名加1、重命名、vue、parseInt、random_js同名重复加(1)

js同名重复加(1)

文章目录


1、html

<div>
	<el-input v-model="inputText" placeholder="请输入"></el-input>
	<el-button
		type="primary"
		plain
		@click="clickAdd"
		style="margin-top: 7px; margin-bottom: 7px"
	>添加</el-button>
	<div>
		<el-table :data="dataContent" style="width: 100%" border>
			<el-table-column prop="id" label="id"> </el-table-column>
			<el-table-column prop="text" label="text"> </el-table-column>
		</el-table>
	</div>
</div>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2、JavaScript

export default {
	data() {
		return {
			inputText: "",
			dataContent: [],
			fileName: {},
		};
	},
	
	methods: {
		// 添加
		clickAdd() {
			let fileName = this.fileName,
				inputText = this.inputText,
				obj = { id: parseInt(Math.random() * 1000000000) };
			
			fileName[inputText] = fileName[inputText] + 1 || 1;
			
			obj.text = fileName[inputText] !== 1 ? inputText + ("(" + (fileName[inputText] - 1) + ")") : inputText;
			
			if (inputText) this.dataContent.push(obj);
		},
	},
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/63505
推荐阅读
相关标签
  

闽ICP备14008679号