2012-08-22 39 views
0

只是想知道我應該在函數insertDash()後面放置什麼(在所有文本框中放置連字符。有二十八個文本框。看起來如果我嘗試:Document.GetElementByName(「bsu」)。value =「 - 」;它不起作用。還有其他建議嗎?Javascript將所有文本框的值更改爲連字符

<form name='table1' method='post' action='u.php'> 
     <script language='javascript'> 
      function insertDash(){ 

      } 
     </script> 
     <table name='hworld' border="1" bordercolor='black'> 
      <tr><th></th> 
       <th>Sunday</th> 
       <th>Monday</th> 
       <th>Tuesday</th> 
        <th>Wednesday</th> 
       <th>Thursday</th> 
       <th>Friday</th> 
       <th>Saturday</th> 
      </tr> 
      <tr> 
       <th>Breakfast</th> 
       <th><input tabindex='1' type='text' name='bsu' id ="bsu" /></th> 
       <th><input tabindex='5' type='text' name='bmo' /></th> 
       <th><input tabindex='9' type='text' name='btu' /></th> 
       <th><input tabindex='13' type='text' name='bwe' /></th> 
       <th><input tabindex='17' type='text' name='bth' /></th> 
       <th><input tabindex='21' type='text' name='bfr' /></th> 
       <th><input tabindex='25' type='text' name='bsa' /></th> 
      </tr> 
       <tr> 
       <th>Lunch</th> 
       <th><input tabindex='2' type='text' name='lsu' /></th> 
       <th><input tabindex='6' type='text' name='lmo' /></th> 
       <th><input tabindex='10' type='text' name='ltu' /></th> 
       <th><input tabindex='14' type='text' name='lwe' /></th> 
       <th><input tabindex='18' type='text' name='lth' /></th> 
       <th><input tabindex='22' type='text' name='lfr' /></th> 
       <th><input tabindex='26' type='text' name='lsa' /></th> 
      </tr> 
      <tr> 
       <th>Dinner</th> 
       <th><input tabindex='3' type='text' name='dsu' /></th> 
       <th><input tabindex='7' type='text' name='dmo' /></th> 
       <th><input tabindex='11' type='text' name='dtu' /></th> 
       <th><input tabindex='15' type='text' name='dwe' /></th> 
       <th><input tabindex='19' type='text' name='dth' /></th> 
       <th><input tabindex='23' type='text' name='dfr' /></th> 
       <th><input tabindex='27' type='text' name='dsa' /></th> 
      </tr> 
      <tr> 
       <th>Snack</th> 
       <th><input tabindex='4' type='text' name='ssu' /></th> 
       <th><input tabindex='8' type='text' name='smo' /></th> 
       <th><input tabindex='12' type='text' name='stu' /></th> 
       <th><input tabindex='16' type='text' name='swe' /></th> 
       <th><input tabindex='20' type='text' name='sth' /></th> 
       <th><input tabindex='24' type='text' name='sfr' /></th> 
       <th><input tabindex='28' type='text' name='ssa' /></th> 
      </tr> 
     </table> 
     <input type='submit' value='Submit'> 
     <input type='button' value='---' onclick="insertDash()"> 
    </form> 

回答

0

使用getElementsByName改變輸入的值與給定的名稱:

document.getElementsByName("bsu")[0].value = "-"; 

使用getElementsByTagName改變值都文本輸入:

function insertDash() { 
    var elements = document.getElementsByTagName("input"); 
    for (var i = 0; i < elements.length; i++) { 
     if (elements[i].type == "text") { 
      elements[i].value = "-"; 
     } 
    } 
} 

DEMO

0

如果你可以使用jQuery,你可以這樣做:

function insertDash() { 
    $('input[type=text]').val('-'); 
}