eclipse 如何从 JSP 中的按钮单击调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15756538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:18:39  来源:igfitidea点击:

How to call a function from a button click in JSP

eclipsestringjspbuttontextbox

提问by Sindu_

I have the following function:

我有以下功能:

<script>
function assign()
{
String val="";
String val = document.form1.text1.value;
System.out.println(val);
}
</script>

i am trying to call the function in the button click as shown below:

我正在尝试调用按钮单击中的函数,如下所示:

  <button type="button" onclick="assign()">Display</button> 

When i click on the button nothing happens. I want the string value in the text box to be printed on the console.Please help

当我点击按钮时,什么也没有发生。我希望将文本框中的字符串值打印在控制台上。请帮忙

回答by Parkash Kumar

First of all, if you are trying to call JavaScript on button click, then your syntax is wrong. See, you are mixing Java with JavaScript (code) in script tag, that is invalid.

首先,如果您尝试在单击按钮时调用 JavaScript,那么您的语法是错误的。看,您在 script 标签中混合了 Java 和 JavaScript(代码),这是无效的。

Use like this:

像这样使用:

<script>
    function assign() {
        var val = "";
        val = document.form1.text1.value;
        alert(val); or console.log(val);
    }
</script>

and

 <button type="button" onclick="javascript:assign();">Display</button>