2012-06-17 73 views
0

嗨我有幾個文本輸入框,並且我有一個清除按鈕,它們都共享相同的名稱,我希望使用一個函數清除它們,並且這樣做是在沒有ID的情況下完成的這佔用了太多空間 我的代碼到目前爲止是這樣的:用一個名字清除多個文本輸入框

var Id; 
var Name; 
function Check(id, name) { 
Id = document.getElementById(id); 
Name = document.getElementById(name); 
if (Id.value == id) { 
    Name.checked = true; 
    alert('correct'); 
    return; 
} else { 
    Name.checked = false; 
    alert('incorrect'); 
} 
} 
function ClearP() { 
document.getElementsByName("input").innerHTML = ''; 
} 

我的HTML看起來像這樣

<div class="content"> 
        <div class="main_story" style="margin-bottom:20px;"> 
         <p class="sentence">the word to go here -></p> 
         <input id="here" name="input" class="sentence" type="text" size="7"> 
         <p class="sentence">is here</p> 
         <button style="float:right" id="correct" onClick="Check('here', 'aShow');">Correct?</button> 
         <input style="float:right" name="aShow" id="aShow" type="checkbox" value=""> 
        </div> 
        <div class="main_story" style="margin-bottom:20px;"> 
         <p class="sentence">the word to go here -></p> 
         <input id="you" name="input" class="sentence" type="text" size="7"> 
         <p class="sentence">is you</p> 
         <button style="float:right" id="correct" onClick="Check('you', 'bShow');">Correct?</button> 
         <input style="float:right" name="bShow" id="bShow" type="checkbox" value=""> 
        </div> 
        <button onClick="ClearP();">Clear</button> 
       </div> 

所有幫助是非常讚賞:)謝謝:)

回答

1
function ClearP(){ 
    var inputs = document.getElementsByTagName("input"); 
    for(var i=0;i<inputs.length;i++) 
     inputs[i].value = ''; 
} 
+0

太感謝你了 這個作品:d! – tht13

0

給他們所有的另一個類(類似「可清除」,也許?),然後找到並清除該類的所有項目。

0

如何使用jQuery,和這樣的事情:

$('[name="input"]').val("");