Java基本问题-按钮单击事件 [英] Basic Javascript question - Button click event

查看:100
本文介绍了Java基本问题-按钮单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是Java语言的初学者,我在下面编写了此代码.
它有一个按钮,一个文本区域.单击按钮时,我调用一个javascript函数,该函数调用数组,对数组进行排序并填充textarea.

按钮onClientclick事件仅在我双击时执行.我希望单击事件可以执行此操作,单击事件将清除文本区域.如何获得单击事件以填充文本区域.

谢谢.

Hi,

I am a beginner in Javascript and I wrote this code below.
It has a button, a text area. When the button is clicked, I call a javascript function that calls an array, sorts the array and populates the textarea.

The button onClientclick event executes only when I double click. I want the single click event to do this, the single click event clears the text area instead. How do I get the single click event to populate the text area.

Thanks.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

</head>
<script language="javascript" type="text/javascript">
    var SortArray = function () {
        var myArr = ["asbestos", "zebra", "grey", "here", "bob"];
        myArr.sort();
        form1.txtJava.value = "";
        for (key in myArr) {
            document.forms[0]['txtJava'].value +=myArr[key];
        };
    };


 </script>
<body>

    <form id="form1" runat="server" >
    <div>

        <textarea name="txtJava" id="txtJava" rows="" cols="" ></textarea>
        <asp:Button ID="Button1" runat="server" OnClientClick="SortArray()" Text="Button" />

    </div>
    </form>

</body>
</html>

推荐答案


因为您的页面正在回发到服务器,这就是您遇到此问题的原因.使用return flase;防止回发.
试试这个:
Hi,
Because your page is posting back to the server that''s why you are facing this problem. Use return flase; to prevent the postback.
Try this:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
 
</head>
<script language="javascript" type="text/javascript">
    var SortArray = function () {
        var myArr = ["asbestos", "zebra", "grey", "here", "bob"];
        myArr.sort();
        form1.txtJava.value = "";
        for (key in myArr) {
            document.forms[0]['txtJava'].value +=myArr[key];
        }
        return false;
    }
 

 </script>
<body>
 
    <form id="form1" runat="server" >
    <div>
 
        <textarea name="txtJava" id="txtJava" rows="" cols="" ></textarea>
        <asp:button id="Button1" runat="server" onclientclick="return SortArray();" text="Button" xmlns:asp="#unknown" />
 
    </div>
    </form>
 
</body>
</html>




--Amit




--Amit


这篇关于Java基本问题-按钮单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆