请教一下,怎么把这段代码换一个优雅的写法

2020-01-13 16:08:06 +08:00
 IMFFA

如下所示,一段拼接的 html 字符串,想要根据接受到的 value 不同,给不同的 radio 加 checked 属性。
这样写实在太丑了
var value = field.value;
var checked1 = "";
var checked2 = "";
var checked3 = "";
var checked4 = "";
var checked5 = "";
var checked6 = "";
var checked7 = "";
switch(value){
case "1":
checked1 = "checked";
break;
case "2":
checked2 = "checked";
break;
case "3":
checked3 = "checked";
break;
case "4":
checked4 = "checked";
break;
case "5":
checked5 = "checked";
break;
case "6":
checked6 = "checked";
break;
case "7":
checked7 = "checked";
break;
}
var html = '<input id="cycle_type" type="radio" name="cycle_type" value="1" '+checked1+' title="日" lay-filter="cycle_type">'+
'<input id="cycle_type" type="radio" name="cycle_type" value="2" '+checked2+' title="周" lay-filter="cycle_type">'+
'<input id="cycle_type" type="radio" name="cycle_type" value="3" '+checked3+' title="半月" lay-filter="cycle_type">'+
'<input id="cycle_type" type="radio" name="cycle_type" value="4" '+checked4+' title="月" lay-filter="cycle_type">'+
'<input id="cycle_type" type="radio" name="cycle_type" value="5" '+checked5+' title="季" lay-filter="cycle_type">'+
'<input id="cycle_type" type="radio" name="cycle_type" value="6" '+checked6+' title="半年" lay-filter="cycle_type">'+
'<input id="cycle_type" type="radio" name="cycle_type" value="7" '+checked7+' title="年" lay-filter="cycle_type">';
return html;

5468 次点击
所在节点    JavaScript
54 条回复
Jackliu
2020-01-13 22:47:07 +08:00
欢迎来我司 因为我司每天统计代码行数
danhahaha
2020-01-13 22:49:15 +08:00
用毛笔?
Dkngit
2020-01-13 23:05:24 +08:00
function getHTML(value) {
const arr = ['日', '周', '半月', '月', '季', '半年', '年'];
let html = "";
arr.forEach((item, index) => {
html += "<input id=\"cycle_type\" type=\"radio\" name=\"cycle_type\" value=\"" + (index + 1) + "\" " + value === (index + 1) ? 'checked' : '' + " title=\"" + item + "\" lay-filter=\"cycle_type\">"
});
return html
}
DFFZMXJ
2020-01-13 23:21:43 +08:00
return ['日', '周', '半月', '月', '季', '半年', '年'].map(
(title, index) => `<input id="cycle_type" type="radio" name="cycle_type" value="${index+1}" ${field.value===index+1?'checked':''} title="日" lay-filter="cycle_type">`
).join('');
Stictonotus
2020-01-13 23:29:11 +08:00
所以为什么要拼接字符串构建 HTML 呢,前端构建字符串 HTML 前期确实能用,但是是一大坨,后期的扩展性可维护性也很低。比如如果你后期要加东西的话,还得考虑前面的逻辑,然后再对着那一坨 HTML 改。但是如果你动态声明,每个属性显而易见,要改什么属性直接在后面加个操作就行了。前面的 #27 #14 都有解决的代码。
但是如果你确实要构建 HTML,那建议你使用循环,一个一个复制粘贴可能是上古写法。
JerryCha
2020-01-14 00:40:23 +08:00
vue.js 请
sugars
2020-01-14 08:34:31 +08:00
@ncwtf 并不是所有浏览器支持 es6 等新的 js 语法,有些浏览器老的版本不兼容,所以才需要使用一个转换的工具就是楼上有人提到的 babel 了,它能将新的 js 语法转换成所有浏览器都支持的最原生的 js 语法
sugars
2020-01-14 08:56:29 +08:00
按楼主的意思写了几个版本,见笑了

beingWH
2020-01-14 09:48:47 +08:00
看了以上的答案,还是觉得 C#才够得上优美两个字。。。
$"<input id='cycle_type' type='radio' name='cycle_type' value='1' {(field.value == "1"?"checked":null)} title='日' lay-filter='cycle_type'><input id='cycle_type' type='radio' name='cycle_type' value='2' {(field.value == "2" ? "checked" : null)} title='周' lay-filter='cycle_type'><input id='cycle_type' type='radio' name='cycle_type' value='3' {(field.value == "3" ? "checked" : null)} title='半月' lay-filter='cycle_type'><input id='cycle_type' type='radio' name='cycle_type' value='4' {(field.value == "4" ? "checked" : null)} title='月' lay-filter='cycle_type'><input id='cycle_type' type='radio' name='cycle_type' value='5'{(field.value == "5" ? "checked" : null)} title='季' lay-filter='cycle_type'><input id='cycle_type' type='radio' name='cycle_type' value='6'{(field.value == "6" ? "checked" : null)} title='半年' lay-filter='cycle_type'><input id='cycle_type' type='radio' name='cycle_type' value='7' {(field.value == "7" ? "checked" : null)} title='年' lay-filter='cycle_type'>"
happy7902
2020-01-14 09:59:47 +08:00
这就是互联网程序员的水平?
lands
2020-01-14 15:04:01 +08:00
用这种写法, 离一年 4 亿行代码的目标又近了一步
vagranth
2020-01-14 15:37:41 +08:00
1. 把那些"日 /周 /月……"常量写在数组里
2. 根据数组,for 循环生成 html
3. 循环时,当 value 匹配,增加 checked 属性
lllllliu
2020-01-14 18:18:35 +08:00
```javascript
/**
* @name createRadioGroup
* @description createRadioGroup
* @author v2er
* @date 2020-01-14
* @param {String} name "Diy"
* @param {Array} data [{title:"date",value:1,checked:false}]
* @returns {String} "HtmlCode"
*/
function createRadioGroup(name,data){
if(data.length == 0) return;
var html = [];
for(var i = 0; i < data.length ; i ++){
var input = window.document.createElement("input")
input.type = "radio"
input.name = name
input.value = data[i].value
input.title = data[i].title
input.defaultChecked = data[i].checked
html.push(input.outerHTML)
}
return html.join("")
}
var data = [
{title:"年",value:1,checked:true},
{title:"月",value:1,checked:false},
{title:"日",value:1,checked:false},
]
createRadioGroup("date",data);
//<input type="radio" name="date" value="1" title="年" checked=""><input type="radio" name="date" value="1" title="月"><input type="radio" name="date" value="1" title="日">"
```
wangkiliaosi8899
2020-01-18 17:08:00 +08:00
看样子楼主深谙命令式编程的精髓;

function resolve(value){
const time = ['日','周','半月','月','季','半年','年'];
if(!Number.isInteger(value) || value>time.length){
throw new Error(`参数错误:需要 1~7 的整数`);
}
return Array.from(Array(time.length)).reduce((result, _c, index)=>{
const template = `<input
id="cycle_type_${index}"
type="radio"
name="cycle_type"
value="${index+1}"
${index+1===value?"checked":''}
title="${time[index]}"
lay-filter="cycle_type">`;
return result+template
},'')
}

console.log(resolve(2));

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/637539

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX