Using quot;if/elsequot; with OnClick(使用“if/else使用 OnClick)
问题描述
首先,是否可以直接在html中使用onclick属性编写if/else语句?如果是这样,为什么我的代码不起作用?
First of all, is it even possible to write if/else statements directly in html with onclick attribute? And if so, why is my code not working?
所以这是一个按钮.Calc.Input.value"指的是文本输入,如果该字段为 0 或空白,我想显示错误消息.如您所见,在所有其他情况下,我希望发生其他一些事情.不过,这无关紧要,因为else"之后的所有内容之前都运行良好.
So this is a button. The "Calc.Input.value" refers to a text input and I want to display an error message if that field is 0 or blank. In all other cases I want some other things to happen as you can see. This is not relevant though, as everything after the "else" worked fine before.
<INPUT TYPE="button" NAME="drop" id="dropbutton" style="background-color:#D1E8D5;" VALUE="
Släpp ankare " OnClick="if ("Calc.Input.value == ''" || "Calc.Input.value == '0'")
{window.alert("Please enter a number");} else
{document.getElementById('dropbutton').value=' Justera
längd ';document.getElementById('length').innerHTML =
Calc.Input.value;document.getElementById('dropbutton').style.cssText = 'background-
color:#FFF6B3;';}">
推荐答案
不要把它放在onclick属性中.
Just DON'T put it in the onclick attribute.
使用
<INPUT TYPE="button" NAME="drop" id="dropbutton" style="background-color:#D1E8D5;" VALUE="Släpp ankare ">
<script>
document.getElementById('dropbutton').onclick = function() {
if (Calc.Input.value == '' || Calc.Input.value == '0') {
window.alert("Please enter a number");
} else {
document.getElementById('dropbutton').value=' Justera längd ';
document.getElementById('length').innerHTML = Calc.Input.value;
document.getElementById('dropbutton').style.backgroundColor = '#FFF6B3';
}
return false;
}
</script>
这篇关于使用“if/else"使用 OnClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用“if/else"使用 OnClick
基础教程推荐
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
