2016-06-07 56 views
0

這裏檢查的聲明將檢查在單選按鈕屬性時,由三元操作

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"> 

這將啓用單選按鈕,試用單引號,雙引號,但不能正常工作,需要幫​​助

回答

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>'; 
+0

狗屎錯誤 – dEL

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>';