0

I've been digging through Stack Overflow for a few days now trying to figure out why my code isn't working. Alas, here I am, asking a very simple question.

I have an HTML form that has a text box and a submit button. You enter a number into the textbox, click the submit button and it's supposed to update a div with information about the number you entered. I know the function works. If I hard code a number into the submit button, it inserts the html properly. But if I change the submit button's onclick to include the text in the textbox, nothing happens.

Any help would be greatly appreciated.

HTML===============================

<form>
<input type="text" name="thenumber" id="thenumber" /> <input type="button" onClick="numbers(document.getElementById('thenumber').value);" value="Click Me!" />
</form>
<div id="message"></div>

JS=================================

var numbers = function(identify){

switch (identify) {

    case '1234':
        document.getElementById("message").innerHTML = "Device 1";
        break;
    case '5678':
        document.getElementById("message").innerHTML = "Device 2";
        break;
    case '9101':
        document.getElementById("message").innerHTML = "Device 3";
        break;
        default:
        document.getElementById("message").innerHTML = "Device Unknown";
};
};

Thanks again for all your help!

4

3 回答 3

1

你少了一个括号

    "numbers(document.getElementById('thenumber').value)"
于 2013-05-27T15:58:15.370 回答
0

好的,尝试复制我的代码。

<input type="text" name="thenumber" id="thenumber" /> 
<input type="button" onClick="numbers(document.getElementById('thenumber').value)" value="Click Me!" />

<div id="message"></div>

var numbers = function(identify){

switch (identify) {

    case '1234':
        document.getElementById("message").innerHTML = "Device 1";
        break;
    case '5678':
        document.getElementById("message").innerHTML = "Device 2";
        break;
    case '9101':
        document.getElementById("message").innerHTML = "Device 3";
        break;
        default:
        document.getElementById("message").innerHTML = "Device Unknown";
};
};

onClick 事件有语法错误

于 2013-05-27T16:14:37.573 回答
0

如果你想在文本框中包含文本,你不必使用 innerHTML,你必须使用 value。

例如:document.getElementById("message").value = "Device Unknown";

于 2013-05-27T16:05:32.260 回答