在oncheckedchanged事件中调用javascript不起作用 [英] calling javascript in oncheckedchanged event dosenot work

查看:71
本文介绍了在oncheckedchanged事件中调用javascript不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有显示和隐藏div的JavaScript函数.我需要在复选框oncheckedchanged事件上调用它,但它总是给我一个错误,这是javascript函数.

Hello everybody I have JavaScript function that show and hide div. I need to call it on checkbox oncheckedchanged event but it always gives me an error here is the javascript function.

<script language="javascript" type="text/javascript">
function showHideDiv(divID)
{

divstyle = document.getElementById("divID").style.visibility;
if(divstyle.toLowerCase()=="visible" || divstyle == "")
{
document.getElementById("divID").style.visibility = "hidden";

}
else
{
document.getElementById("divID").style.visibility = "visible";

}
}
</script>


这是我的称呼方式:


and here is how I called it:

<td class="style2">
                 <asp:CheckBox ID="YES" runat="server" AutoPostBack="True"

            Text="yes" oncheckedchanged="showHideDiv(divID);" />

             </td>
             <td class="style2">
               <asp:CheckBox ID="NO" runat="server"  AutoPostBack="True"

                    Text="no" oncheckedchanged="showHideDiv(divID);"/>
             </td>




这是我的div:




and here is my div:

<div id="Comments"   >
                  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                  </div>




我想默认隐藏它

还有另一个问题,我想取消选中其他复选框后,我需要取消选中该复选框,我需要使用c#,现在我可以调用javascript函数然后执行c#代码
任何人都可以提供帮助,
在此先感谢




and I want to make it hiddden by default

and there is another question I want to uncheck check box after the other is checked i need to make it using c#, now can i call the javascript function then execute c# code
any one can help ,
thanks in advance

推荐答案

OnCheckedChanged 是服务器端事件.因此,您不允许在此处提供javascript函数.

您需要从服务器注册javascript或使用

输入type ="CheckBox"并完全删除runat = server.

< asp:CheckBox OnCheckedChanged ="checkchanged" runat ="server" AutoPostBack = true/>

OnCheckedChanged is a Server side event. So you are not allowed to give the javascript function here.

You need to register the javascript from the server or use

input type="CheckBox" and remove runat=server totally.

<asp:CheckBox OnCheckedChanged="checkchanged" runat="server" AutoPostBack=true/>

protected void checkchanged(object sender, EventArgs e)
       {
           this.ClientScript.RegisterStartupScript(this.GetType(), "registerscript", "alert('hi');", true);
       }




因此,您可以看到回发会产生警报.




Hence you can see the postback produces the alert.


您遇到什么错误?

您必须先修改以下代码块.
What error are you getting ?

You have to chage the below codeblock first.
<asp:CheckBox ID="YES" runat="server" AutoPostBack="True"

           Text="yes" oncheckedchanged="showHideDiv(divID);" />
            </td>



您必须通过您的DIVID,即注释".应该像



You have to Pass your DIVID, Which is "Comments". It should be like

oncheckedchanged="showHideDiv(Comments);"



与此更改代码,让我知道是否还有其他问题.

谢谢!



Change the code with this and let me know if you have any other issue.

Thanks !


谢谢,我解决了这个问题,这很容易,但是我没有集中精力:
那是我使用的代码,错误不在oncheckedchanged上,我必须在单击上使用
thanks i solve the problem it was easy but i was not focus:
that is the code i use ,the error was not on oncheckedchanged ,i have to use on click
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="java.WebUserControl1" %>
<style type="text/css">
    .style1
    {
        width: 100%;
    }
</style>
<script language="JavaScript" type="text/javascript">
    function toggle(id) {
        var state = document.getElementById(id).style.display;
        if (state == 'block') {
            document.getElementById(id).style.display = 'none';
        } else {
            document.getElementById(id).style.display = 'block';
        }
    }
    function hide(id) {
     var state = document.getElementById(id).style.display;
     if (state == 'block') {
         document.getElementById(id).style.display = 'none';
     }
    }
    </script>
<table class="style1">
    <tr>
        <td>
            &nbsp;</td>
        <td>
          <asp:checkbox id="no" OnClick="JavaScript:hide('Comments');" Runat="server" />
        </td>
        <td>
          <asp:checkbox id="yes" OnClick="JavaScript:toggle('Comments');" Runat="server" />
        </td>
    </tr>
    <tr>
        <td colspan="3">
                  <div id="Comments" style="display:none"  >
                  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                  </div>
         </td>
    </tr>
</table>


这篇关于在oncheckedchanged事件中调用javascript不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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