如何使用javascript检查文本框onkeypress事件上的复选框 [英] how to check the check box on textbox onkeypress event using javascript

查看:61
本文介绍了如何使用javascript检查文本框onkeypress事件上的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,如果我在文本框中输入数据,则应选中复选框.......以下是我的代码...



< pre lang =xml> < asp:GridView ID = GridView1 ShowHeader = False runat = server 宽度 = 95%

< span class =code-attribute> AutoGenerateColumns = False DataSourceID = SqlDataSource1

DataKeyNames = DxID ondatabound = GridView1_DataBound

onrowcreated = GridView1_RowCreated onrowdatabound = GridView1_RowDataBound

onselectedindexchanged = GridView1_SelectedIndexChanged CellSpacing = 5

CssClass = grid onrowcommand = GridView1_RowCommand >
< AlternatingRowStyle CssClass = alternatingItemStyle / >
< HeaderStyle CssClass = Headerstyle / >
< RowStyle CssClass = RowStyle < span class =code-keyword> / >
< SelectedRowStyle CssClass = SelectedRowStyle / >
< >
< asp:TemplateField >
< ItemTemplate >
< span class = style3 > < asp:CheckBox ID = chkSelect runat = server 已检查 =' <%#GetValue(Eval( IsCheck)。ToString())%> ' / > < / span >

< asp:TextBox ID = TextBox2 runat = server 宽度 = 95% 高度 = 85px

TextMode = MultiLine CssClass = inpt_a 字体名称 = Open Sans,sans-serif onkeypress = check(this) 文字 =' <% #Bind( Plan4%> ' > < / asp:TextBox >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< HeaderStyle 高度 = 0px / >
< / asp:GridView >

解决方案

尝试这样的onkeyup事件:

 < %@     Page    < span class =code-attribute>语言  =  C#    AutoEventWireup   =  true    CodeFile   =  Default2.aspx.cs   继承  =  Default2   %>  
< < span class =code-leadattribute>!DOCTYPE html PUBLIC - / / W3C // DTD XHTML 1.0 Transitional // EN http://www.w3.org/TR/xhtml 1 / DTD / xhtml1-transitional.dtd >
< html xmlns = http://www.w3.org/1999/xhtml >
< head runat = server >
< title > < / title < span class =code-keyword>>
< script type = text / javascript >
function check(){
// 删除前导和尾随间距
var textboxcontent = document .getElementById( <%= TextBox1.ClientID%>)。value.trim();
document .getElementById( <% = CheckBox1.ClientID%>)。checked =(textboxcontent.length> 0 );
}
< / 脚本 >
< / head >
< body >
< 表单 id = form1 runat < span class =code-keyword> = server >
< div >
< asp:CheckBox ID = CheckBox1 runat = 服务器 / >
< / div >
< p >
< asp:TextBox ID = TextBox1 runat = 服务器 onkeyup = check() > < / asp:TextBox >
< / p >
< / form >
< / body >
< / html >


Actually if i entered data in text-box means check box should be checked....... and below is my code...

<asp:GridView ID="GridView1" ShowHeader="False" runat="server" Width="95%"

                AutoGenerateColumns="False" DataSourceID="SqlDataSource1"

                DataKeyNames="DxID" ondatabound="GridView1_DataBound"

            onrowcreated="GridView1_RowCreated" onrowdatabound="GridView1_RowDataBound"

            onselectedindexchanged="GridView1_SelectedIndexChanged" CellSpacing="5"

            CssClass="grid" onrowcommand="GridView1_RowCommand"  >
                    <AlternatingRowStyle CssClass="alternatingItemStyle" />
                    <HeaderStyle CssClass="Headerstyle" />
                    <RowStyle  CssClass="RowStyle"/>
                    <SelectedRowStyle CssClass="SelectedRowStyle" />
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                                       <span class="style3"><asp:CheckBox ID="chkSelect" runat="server" Checked= '<%# GetValue(Eval("IsCheck").ToString()) %>'  /></span>

                                    <asp:TextBox ID="TextBox2" runat="server" Width="95%" Height="85px"

                                        TextMode="MultiLine" CssClass="inpt_a" Font-Names="Open Sans , sans-serif"   onkeypress="check(this)"   Text='<%# Bind("Plan4") %>'></asp:TextBox>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <HeaderStyle Height="0px" />
            </asp:GridView>

解决方案

Try onkeyup event like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script  type="text/javascript">
    function check() {
            // remove leading and trailing spacing
        var textboxcontent = document.getElementById("<%=TextBox1.ClientID%>").value.trim();
            document.getElementById("<%=CheckBox1.ClientID%>").checked = (textboxcontent.length > 0);
        }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBox ID="CheckBox1" runat="server" />
    </div>
    <p>
        <asp:TextBox ID="TextBox1" runat="server" onkeyup="check()"></asp:TextBox>
    </p>
    </form>
</body>
</html>


这篇关于如何使用javascript检查文本框onkeypress事件上的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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