ruleForm.value.substring()
时间: 2024-06-07 21:07:27 浏览: 29
`ruleForm.value.substring()` 是 JavaScript 中字符串对象的一个方法,用于从原始字符串中提取子字符串。该方法接受两个参数:起始位置和结束位置。起始位置是你想要提取的子字符串的第一个字符的位置,结束位置是你想要提取的子字符串的最后一个字符的位置。例如,如果你想从字符串 `ruleForm.value` 中提取从第二个字符到第五个字符的子字符串,可以使用 `ruleForm.value.substring(1, 5)`。这将返回一个新的字符串,其中包含原始字符串的第二个字符到第五个字符之间的所有字符。
相关问题
function checkYearLength() { const dateInput = document.getElementById("date"); const year = dateInput.value.substring(0, 4); if (year.length > 4) { const correctedValue = year.substring(0, 4) + dateInput.value.substring(4); dateInput.value = correctedValue; } }有bug,判断不了有没有超过4个
这个函数的问题在于它只检查了字符串的前四个字符,而不是整个字符串。所以,当输入的年份超过4位数时,它就会把整个字符串截断,只保留前四个字符。这个问题可以通过将 year.length 改为 dateInput.value.length 来解决。修改后的代码如下:
```
function checkYearLength() {
const dateInput = document.getElementById("date");
const year = dateInput.value.substring(0, 4);
if (dateInput.value.length > 4) {
const correctedValue = year.substring(0, 4) + dateInput.value.substring(4);
dateInput.value = correctedValue;
}
}
```
这样,当输入的字符串长度超过4时,函数就会把整个字符串保留下来,而不是截断。
const getClassifyData = ()=>{ let pararms = {name: nodeLable.value} getClassify(pararms).then(res=>{ TypeSelects.value = res.result if(parId.value){ return getClassify({classId:parId.value}) } }).then(res=>{ childOptions.value = res.result if(claId.value){ return getClassify({ indexId: claId.value }) } }).then(res=>{ lastResult.value = res.result tableData.value = res.result.map(obj => { const dashIndex = obj.indexFinalName.indexOf("-"); const dataType = obj.indexFinalName.substring(0, dashIndex) + "-基础数据"; return { ...obj, dataType }; }) }).catch(error=>{ console.log('error', error) }) }改进代码
这段代码可以进行一些改进,例如:
1. 使用 async/await 替代 Promise 的 then() 和 catch() 方法,使代码更易读。
2. 将函数参数改为传入 pararms 对象,使代码更灵活。
3. 使用 try/catch 捕获错误,而不是使用 catch() 方法。
改进后的代码如下:
```
const getClassifyData = async (params) => {
try {
const res1 = await getClassify({ name: params.name });
TypeSelects.value = res1.result;
if(params.parId){
const res2 = await getClassify({ classId: params.parId });
childOptions.value = res2.result;
if(params.claId){
const res3 = await getClassify({ indexId: params.claId });
lastResult.value = res3.result;
tableData.value = res3.result.map(obj => {
const dashIndex = obj.indexFinalName.indexOf("-");
const dataType = obj.indexFinalName.substring(0, dashIndex) + "-基础数据";
return { ...obj, dataType };
});
}
}
} catch (error) {
console.log('error', error);
}
}
```
这样就可以更简洁地实现同样的功能,并且代码更易读。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)