2014-02-24 25 views

回答

1

如果這是純JavaScript,你會使用類似:

var inputs = document.getElementById('myForm').getElementsByTagName('input'), 
    i = inputs.length - 1; //Loop backwards to avoid issues when removing an input 
    for(; i >= 0; i--) { //for loop 
    //Is this an unchecked radio button 
    if(inputs[i].type === 'radio' && !inputs[i].checked) { 
     //Remove this input element 
     inputs[i].parentNode.removeChild(inputs[i]); 
    } 
} 

UPDATE

每用戶的評論,它會出現他正嘗試刪除包含radio輸入的整個label元素。以下代碼將完成此操作。

var inputs = document.getElementsByTagName('input'), 
    i = inputs.length - 1, 
    item; 
//Loop backwards through items 
for(; i >= 0; i--) { 
    //If this is an unchecked radio button 
    if(inputs[i].type === 'radio' && !inputs[i].checked) { 
     //Begin trying to find the label item 
     item = inputs[i].parentNode; 
     //Is this the label item 
     while(item && item.nodeName.toLowerCase() !== 'label') { 
      item = item.parentNode; 
     } 
     //If an item was found 
     if(item && item.parentNode) { 
      //Remove the label element 
      item.parentNode.removeChild(item); 
     } 
    } 
} 
+0

似乎不起作用。這裏是我正在處理的頁面的鏈接。 http://adventures.org/quiz/default_post.asp – justincron

+0

@justincron我剛剛在您的頁面上運行了精確的代碼,它確實刪除了您在文章中描述的單選按鈕。你是否想要移除整個答案? – Kyle

+0

是的我想刪除單選按鈕並回答與它連接。 – justincron

0

你可以這樣做:

var yourForm = document.getElementById("yourFormId"); 
var removeUnchecked = function (form) { 
    var childrenList = form.children, 
     num = childrenList.length - 1; 
    for (var i = 0; i >= num; i--) { 
    if (!childrenList[i].checked) { 
     form.removeChild(childrenList[i]); 
    } 
    } 
} 

removeUnchecked(yourForm); 

與你的表單元素的id第一線更換id

+0

似乎不起作用。這裏是我正在處理的頁面的鏈接。 http://adventures.org/quiz/default_post.asp – justincron

+0

我現在會修改它(當我發佈它時我無法修改)並更新我的答案。 – dieortin

相關問題