检查CheckBox是否在Javascript中具有BorderColor [英] Check if CheckBox has BorderColor in Javascript

查看:67
本文介绍了检查CheckBox是否在Javascript中具有BorderColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview,其中包含复选框。

我在OnRowDataBound事件后面的代码中编写了以下代码 -

 CheckBox control2 = e.Row.FindControl(  chk830 as  CheckBox; 
if null != control2)
{
bool chkd = control2.Checked;
if (chkd)
{
control2.BorderWidth = 2 ;
control2.BorderColor = System.Drawing.Color.Red;
}
}





复选框有OnClick =chkclick(this)javascript。



 < script    类型  =  text / javascript >  

var num1 = ;
function chkclick(obj){


if (num1!= ){
if (document.getElementById(num1)。 已检查 ||(document.getElementById(num1)。已检查 == false )){
document.getElementById(num1)。已检查 = false ;


if (document.getElementById(num1.substring( 0 ,num1.indexOf( chkV))+ chk830)。style.BorderColor ==
document.getElementById(num1.substring( 0 ,num1.indexOf( chkV))+ chk830)。已检查 = false ;
}
}
}
< / script >



这不起作用----

  if (document.getElementById(num1.substring( 0 ,num1.indexOf(< span class =code-string>  chkV))+   chk830)。style.BorderColor ==  



i也试过 -

 如果(document.getElementById(num1.substring( 0 ,num1.indexOf(quot; chkV  < span class =code-string>))+ chk830  )。borderColor ==   





我想要的是如果它没有边框颜色则取消选中该复选框......我怎么能实现这个?

:(我想完成这个直到明天..请给我一些建议..

解决方案

< blockquote>使用cssclass复选框边框,边框颜色等

&检查javascript中的classname,如下面的代码所示,

我测试了以下代码&它根据您的要求工作,

验证和评论。



 <   head     runat   = 服务器 >  
< title > Untitled Page < / title >
< style >
.TestClass1
{
border:2px solid red;
padding-top:4px;
}
.TestClass2
{
/ *
border:2px solid black;
padding-top:4px;
* /
}
< / style >
< script type = text / javascript >

function chkclick(obj){
var num1 = obj。 ID;
if(num1!=){
if(document.getElementById(num1).checked ||(document.getElementById(num1).checked == false)){
document。 getElementById(num1).checked = false;


if(document.getElementById(num1.replace(chkV,chk830))。parentElement.className ==TestClass1){
document.getElementById( num1.replace(chkV,chk830))。parentElement.className =TestClass2;
document.getElementById(num1.replace(chkV,chk830))。checked = false;
}
else {
document.getElementById(num1.replace(chkV,chk830))。parentElement.className =TestClass1;
document.getElementById(num1.replace(chkV,chk830))。checked = true;
}
}
}
}
< / script >
< / head >
< body >
< 表格 id = form1 runat = 服务器 >
< div >
< asp:GridView ID = GridView1 runat = server OnRowDataBound = GridView1_RowDataBound >
< >
< asp:TemplateField >
< ItemTemplate >
< asp:CheckBox ID = chkV < span class =code-attribute> runat = server onclick = chkclick(this) / >
< / ItemTemplate > ;
< / asp:TemplateField >
< asp:TemplateField >
< ItemTemplate >
< asp:CheckBox ID = chk830 runat = server onclick = chkclick(this) 已检查 = true / >
< / ItemTemplate >
< / asp:TemplateField > ;
< /列 >
< / asp:GridView >

< / div >
< / form >
< / body >







更改OnRowDataBound

  protected   void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox control2 = e.Row.FindControl ( chk830 as CheckBox;
if null != control2)
{
if (control2.Checked)
{
control2.Attributes.Add( class TestClass1);
// control2.BorderWidth = 2;
// control2.BorderColor = System.Drawing.Color.Red;
}
}
}
}


I have a gridview which consist checkbox in it.
I have written the following code in my code behind in OnRowDataBound event--

CheckBox control2 = e.Row.FindControl("chk830") as CheckBox;
        if (null != control2)
        {
            bool chkd = control2.Checked;
            if (chkd)
            {
                control2.BorderWidth = 2;
                control2.BorderColor = System.Drawing.Color.Red;
            }
        }



the checkbox has OnClick="chkclick(this)" javascript.

<script type="text/javascript">

    var num1 = "";
    function chkclick(obj) {

       
            if (num1 != "") {
if (document.getElementById(num1).checked ||(document.getElementById(num1).checked==false)) {
                    document.getElementById(num1).checked = false;
                    
              
                    if (document.getElementById(num1.substring(0, num1.indexOf("chkV")) + "chk830").style.BorderColor == "")
                        document.getElementById(num1.substring(0, num1.indexOf("chkV")) + "chk830").checked = false;
      }
    }
   }
  </script>


this doesnt work----

if (document.getElementById(num1.substring(0, num1.indexOf("chkV")) + "chk830").style.BorderColor == "")


i also tried--

if (document.getElementById(num1.substring(0,num1.indexOf(quot;chkV")) + "chk830").borderColor == "")



what i want2do is uncheck the checkbox if it does not have border color...how can i achieve this??
:( i want to complete this till tommorrow.. plz suggest me something..

解决方案

Use cssclass for checkbox borders, border colors etc
& check for the classname in javascript as placed in the below code,
i have tested the below code & its working as per your requirement,
verify and comment.

<head runat="server">
    <title>Untitled Page</title>
    <style>
        .TestClass1
        {
            border: 2px solid red;
            padding-top: 4px;
        }
        .TestClass2
        {
            /*
            border: 2px solid black;
            padding-top: 4px;
            */
        }
    </style>
    <script type="text/javascript">

        function chkclick(obj) {
            var num1 = obj.id;
            if (num1 != "") {
                if (document.getElementById(num1).checked || (document.getElementById(num1).checked == false)) {
                    document.getElementById(num1).checked = false;


                    if (document.getElementById(num1.replace("chkV", "chk830")).parentElement.className == "TestClass1") {
                        document.getElementById(num1.replace("chkV", "chk830")).parentElement.className = "TestClass2";
                        document.getElementById(num1.replace("chkV", "chk830")).checked = false;
                    }
                    else {
                        document.getElementById(num1.replace("chkV", "chk830")).parentElement.className = "TestClass1";
                        document.getElementById(num1.replace("chkV", "chk830")).checked = true;
                    }
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField >
                    <ItemTemplate>
                        <asp:CheckBox ID="chkV" runat="server" onclick="chkclick(this)" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField >
                    <ItemTemplate>
                        <asp:CheckBox ID="chk830" runat="server" onclick="chkclick(this)" Checked="true" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

    </div>
    </form>
</body>




Change the OnRowDataBound

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
           CheckBox control2 = e.Row.FindControl("chk830") as CheckBox;
           if (null != control2)
           {
               if (control2.Checked)
               {
                   control2.Attributes.Add("class", "TestClass1");
                   //control2.BorderWidth = 2;
                   //control2.BorderColor = System.Drawing.Color.Red;
               }
           }
       }
   }


这篇关于检查CheckBox是否在Javascript中具有BorderColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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