0

.innerhtmlを使用して、ボタンの上にエラーメッセージを入力しようとしています。私はこれを機能させるのに近づいていると思いますが、行き詰まっています。私はJSを初めて使用するので、非常に単純なセマンティックエラーがあると思います。

これが私のjsfiddleです:http://jsfiddle.net/ajX2U/

var sel  = document.getElementById("stateType");

    switch(sel.value) {
        case "VA":
            location.href = "http://www.test1.com";
            break;
        case "MD":
            location.href = "http://www.test2.com";
            break;
        case "IL":
            location.href = "http://www.test3.com";
            break;
        case "TX":
            location.href = "http://www.test4.com";
            break;
        case "other":
            location.href = "http://www.test5.com";
            break;
        // oh dear we have selected a wrong option
        default:
            document.getElementById('error').innerHTML = 'Fred Flinstone';
    }
}
4

2 に答える 2

0

フィドルに関数ヘッドを含めるのを忘れただけです。それを追加した後、それは私のために働いた。

更新されたフィドル

function validate(){
    var sel  = document.getElementById("stateType");

    switch(sel.value) {
        case "VA":
            location.href = "http://www.test1.com";
            break;
        case "MD":
            location.href = "http://www.test2.com";
            break;
        case "IL":
            location.href = "http://www.test3.com";
            break;
        case "TX":
            location.href = "http://www.test4.com";
            break;
        case "other":
            location.href = "http://www.test5.com";
            break;
        // oh dear we have selected a wrong option
        default:
            document.getElementById('error').innerHTML = 'Fred Flinstone';
    }
}
于 2012-12-06T16:12:22.157 に答える
0

あなたのフィドルから、これはそれがうまくいかないところです。

(Chromeを使用してコンソールを開くと表示されます)

<script type='text/javascript'>//<![CDATA[ 
window.addEvent('load', function() {
var sel  = document.getElementById("stateType");

switch(sel.value) {
    case "VA":
        location.href = "http://www.test1.com";
        break;
    case "MD":
        location.href = "http://www.test2.com";
        break;
    case "IL":
        location.href = "http://www.test3.com";
        break;
    case "TX":
        location.href = "http://www.test4.com";
        break;
    case "other":
        location.href = "http://www.test5.com";
        break;
    // oh dear we have selected a wrong option
    default:
        document.getElementById('error').innerHTML = 'Fred Flinstone';
}
}
var sel  = document.getElementById("stateType");
});//]]>

var selが間違った場所にあり、それを壊します(とにかく2番目のvar sel)。

于 2012-12-06T16:14:31.873 に答える