1

只是想知道我应该在函数 insertDash(){ 之后放置什么以在所有文本框中放置一个连字符。有二十八个文本框。好像如果我尝试: Document.GetElementByName("bsu").value="-"; 它不起作用。还有其他建议吗?

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

2 回答 2

1

如果你可以使用 jQuery,你可以这样做:

function insertDash() {
    $('input[type=text]').val('-');
}
于 2012-08-22T23:21:18.870 回答
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 = "-";
        }
    }
}

演示

于 2012-08-22T23:15:33.373 回答