在由三元运算符

时间:2016-06-07 10:12:19

标签: jquery radio-button

这是声明

content += '<tr>
            <td>Tier 
            <input type="radio" name="'qid+'tier'+(i+1)+'" "' +((des4=="1")? "checked" : "") +'" value=1 >1
            <input type="radio" name="'qid+'tier'+(i+1)+'" "' +((des4=="2")? "checked" : "") +'" value=2 >2
            <input type="radio" name="'qid+'tier'+(i+1)+'" "' +((des4=="3")? "checked" : "") +'" value=3 >3
            <input type="radio" name="'qid+'tier'+(i+1)+'" "' +((des4=="4")? "checked" : "") +'" value=4 >4<td></tr>';

这会给输出一些像这样的东西

<input type="radio" "checked" name="q12tier1" value="2">

而不是

<input type="radio" checked name="q12tier1" value="2">

这将启用单选按钮,尝试单引号,双引号,但不工作,需要帮​​助

2 个答案:

答案 0 :(得分:0)

你只需删除三元组周围的双引号即可。您在+之前还错过了qid。试试这个:

content += '<tr>
        <td>Tier 
        <input type="radio" name="'+qid+'tier'+(i+1)+'" ' +((des4=="1")? 'checked="checked"': "") +' value=1 >1
        <input type="radio" name="'+qid+'tier'+(i+1)+'" ' +((des4=="2")? 'checked="checked"' : "") +' value=2 >2
        <input type="radio" name="'+qid+'tier'+(i+1)+'" ' +((des4=="3")? 'checked="checked"' : "") +' value=3 >3
        <input type="radio" name="'+qid+'tier'+(i+1)+'" ' +((des4=="4")? 'checked="checked"' : "") +' value=4 >4<td></tr>';

另请注意,最好在循环中创建HTML以停止重复代码:

var content = '';
for (var i = 1; i <= 4; i++) {
    content += '<input type="radio" name="' + qid + 'tier' + i +'" ' + (des4 == i ? 'checked="checked"' : "") + ' value="' + i + '">' + i
}
content = '<tr><td>Tier' + content + '</td></tr>';

答案 1 :(得分:0)

删除id consitions之前和之后的双引号,尝试:

content += '<tr>
            <td>Tier 
            <input type="radio" name="'qid+'tier'+(i+1)+'" ' +((des4=="1")? "checked" : "") +' value=1 >1
            <input type="radio" name="'qid+'tier'+(i+1)+'" ' +((des4=="2")? "checked" : "") +' value=2 >2
            <input type="radio" name="'qid+'tier'+(i+1)+'" ' +((des4=="3")? "checked" : "") +' value=3 >3
            <input type="radio" name="'qid+'tier'+(i+1)+'" ' +((des4=="4")? "checked" : "") +' value=4 >4<td></tr>';