简体   繁体   中英

No function when submit button is clicked

Here's my code:

<head>

<script>input[type=text] {
    padding:5px;
    border:2px solid #000080;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
input[type=text]:focus {
    border-color:#ccc;
}
-webkit-border-radius: 5px;
 border-radius: 5px;

}
.rows {
    text-align: center;
}</script>

<script>var btn = document.getElementsByTagName("button")[0];
var inpt = document.getElementsByName("post")[0];

function openBox() {
   document.getElementById('postBox').style.display = "block";
}

btn.onclick = function () {

    if (!inpt.value) alert("Please enter something to post.");

    var tbl = document.getElementsByTagName("table")[0];
    var row = tbl.insertRow(0);
    var cell = row.insertCell(0);
    var txt = document.createTextNode(inpt.value);
    cell.appendChild(txt);
    tbl.insertRow(0);
    tbl.insertRow(0);

    inpt.value = "";

};</script>

</head>

<body>

<input type="button" name="answer" value="post" onclick="openBox()" />

<div id="postBox" style="display:none;">
<center>
    <input type="text" name="post" maxlength="100" />
    <br>
    </br>
    <button style="border : solid 0px #000080; border-radius : 4px; moz-border-radius : 4px; -webkit-box-shadow : 0px 0px 5px rgba(0, 0, 0, 1.0); -moz-box-shadow : 0px 0px 5px rgba(0,0,0,1.0); box-shadow : 0px 0px 5px rgba(0,0,0,1.0); font-size : 24px; font-style : ;color : #ffffff; padding : 4px 10px; background-color : #000080;">post</button>
    <br/>
    <br/>
    <center>
    <table class="rows"></table>
</center>
</center>
</div>

</body>

</html>

Everything is fine, but when you go to "post" something, no action is taken, unlike in my JSFiddle below.

My JSFiddle ( http://jsfiddle.net/yqh2rqh0/25/ ) is working perfectly how it is supposed to be working but the web paged version is not.

the first issue:

Uncaught SyntaxError: Unexpected token {

is because your css is being called with <script></script> tags instead of <style></style>

<script> <-------- change to style
input[type=text] {
    padding:5px;
    border:2px solid #000080;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
input[type=text]:focus {
    border-color:#ccc;
}
-webkit-border-radius: 5px;
 border-radius: 5px;

}
.rows {
    text-align: center;
}
</script> <----- change to style

The second issue:

Uncaught TypeError: Cannot set property 'onclick' of undefined

is because your javascript needs to be loaded at the bottom of the page right before </body> instead of the top in the <head></head> (just like your fiddle is loading No wrap - In <body>)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM