如何将功能应用于按钮
问题描述:
我有4个下拉列表。每个下拉列表都包含所有虚假值和一个真实值,我的目标是生成一条警报消息,并根据值为真来打开新页面。但是,我想知道如何将此代码应用于按钮,以便在点击按钮时发生所描述的操作,因为此时绝对没有任何事情发生。如何将功能应用于按钮
<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>
答
确保您的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!");
}
}
祝你好运!
即使当我点击按钮,我仍然没有得到任何东西,代码的按钮需要实际上所有的if语句是什么? – user1243542 2012-03-04 13:43:42
尝试添加一些alert()语句以查看问题是什么。函数实际上是否被调用?选择框变量是否有值?还要确保检查JavaScript的错误日志以获取有关错误的一些指示。 – Wesley 2012-03-04 13:51:30