如何将功能应用于按钮

时间:2012-03-04 13:22:38

标签: javascript javascript-events

我有4个下拉列表。每个下拉列表都包含所有错误值和一个真值,我的目标是生成警报消息并根据值为true打开新页面。但是,我想知道如何将此代码应用于按钮,以便在单击按钮时发生所描述的操作,因为此刻绝对没有任何反应。

<input name="button" type="submit" class="main" id="button" value="Submit" onclick="submit()" />


</script>

<script type="javascript">

这是我的按钮的详细信息,放在这里供您参考

<input name="button" type="submit" class="main" id="button" value="Submit" onclick="submit()" />

var selectbox = document.getElementById("select_box"); 
var a = selectbox.options[selectbox.selectedIndex].value;

var selectbox2 = document.getElementById("select_box2"); 
var b = selectbox2.options[selectbox2.selectedIndex].value;

var selectbox3 = document.getElementById("select_box3"); 
var c = selectbox3.options[selectbox3.selectedIndex].value;

var selectbox4 = document.getElementById("select_box4"); 
var d = selectbox4.options[selectbox4.selectedIndex].value;


if (a == "true" && b == "true" && c == "true" && d == "true")
{
alert("Correct you have won press OK for your Reward!")
document.open("Reward.html");
}
else
{
alert("Not right Please try again!");
}

</script>

1 个答案:

答案 0 :(得分:0)

确保您的onClick值指向如下函数:

<input name="button" type="submit" class="main" id="button" value="Submit" onclick="checkValues();"/>

功能确实存在:

function checkValues(){
var selectbox = document.getElementById("select_box"); 
var a = selectbox.options[selectbox.selectedIndex].value;

var selectbox2 = document.getElementById("select_box2"); 
var b = selectbox2.options[selectbox2.selectedIndex].value;

var selectbox3 = document.getElementById("select_box3"); 
var c = selectbox3.options[selectbox3.selectedIndex].value;

var selectbox4 = document.getElementById("select_box4"); 
var d = selectbox4.options[selectbox4.selectedIndex].value;

if (a == "true" && b == "true" && c == "true" && d == "true")
    {
        alert("Correct you have won press OK for your Reward!")
        document.open("Reward.html");
    }
    else
    {
        alert("Not right Please try again!");
    }
}
祝你好运!