复选框自动检查文本框上的数据为true,不检查为false [英] Checkbox auto check when the data on textbox is true and uncheck when it is false

查看:94
本文介绍了复选框自动检查文本框上的数据为true,不检查为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里遇到了问题.如您所见,我正在数据库中收集我的数据

got a problem here. As you see im gathering my data on my database

Dim query As String
Dim connection As SqlConnection = New SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
        query = "Select * from [tried] WHERE ([Username] LIKE ''%'' + @Username + ''%'')"
        connection.Open()
        Dim bind As SqlCommand = New SqlCommand(query, connection)
        bind.Parameters.AddWithValue("@Username", ddusername1.SelectedValue)
        Dim DataReader As SqlClient.SqlDataReader = bind.ExecuteReader()
        Do While DataReader.Read()
            textbox1.Text = (DataReader("V15029").ToString)
etc

现在我想要的是,当textbox1.text ="True"的值时,将自动检查checkbox1,如果它为false,则将其取消选中
我正在做一些研究,但是以某种方式没有帮助.请帮助我.我只是这里的初学者.
我的试用代码如下,但它不起作用

now i want is that when the value of textbox1.text = "True" then checkbox1 will be automatically be checked and if it is false then it will be unchecked
Im doing some research but somehow it doesnt help.. Plss help me.. Im just a beginner here.
my trial code are as follows but it doesnt work

function check()
{
document.GetElementById("checkboxname").checked = true ;
}

感谢并提供更多功能

推荐答案

在Javascript中,您可以处理文本框的KeyUp事件.查看您当前的代码,它将无法正常工作,因为您尚未添加检查文本框值的条件.假设您有这样的ASP标记:

In Javascript, you can handle the KeyUp event of your textbox. Looking at your current code, it wont work because you havent added a condition that checks for the value of your textbox. Suppose you have your ASP tags like this:

<asp:TextBox ID="txt" runat="server" onkeyup="keyUp();"></asp:TextBox>
<asp:CheckBox ID="chk" runat="server" />


注意如何将文本框的onkeyup事件设置为我们的Javascript函数

您可以在Javascript中执行类似的操作.


Notice how I set the onkeyup event of textbox to our Javascript function

You can do something like this in Javascript.

function keyUp() {
            if (document.getElementById("txt").value == "") {
                document.getElementById("chk").checked = false;
            }
            else {
                document.getElementById("chk").checked = true;
            }
        }



[更新]

要将其重复用于多个控件,您需要传递事件对象以及与文本框配对的复选框的名称.请参见下面的JS函数.



[UPDATE]

To reuse this for multiple controls, you need to pass the event object and the name of the checkbox to which the textbox is paired to. See the JS function below.

function keyUp(e, nameOfCheckBox) {
            if (e.target.value == "") {
                document.getElementById(nameOfCheckBox).checked = false;
            }
            else {
                document.getElementById(nameOfCheckBox).checked = true;
            }
        }



然后您可以像这样在ASPX上调用它.



And then you can call it on your ASPX like this.

<asp:TextBox ID="txt1" runat="server" onchange="keyUp(event, 'chk1');"></asp:TextBox>
       <asp:CheckBox ID="chk1" runat="server" />
<asp:TextBox ID="txt2" runat="server" onchange="keyUp(event, 'chk2');"></asp:TextBox>
       <asp:CheckBox ID="chk2" runat="server" />
      <!--- etc --- >



要回答有关OnTextChanged的问题,是的,文本框具有该事件.但是,这是一个服务器事件,我宁愿选择OnChange方法代替它.您可以将onkeyup事件更改为onchange,并且JS函数仍将起作用.与该方法的唯一区别是,该函数将在您离开文本框并移至另一个控件时执行.但这取决于您的偏好.



To answer your question about OnTextChanged, yes a textbox has that event. However, this is a server event and I would rather choose the OnChange method instead of it. You can change the onkeyup event to onchange and the JS function will still work. The only difference with that method is the function will execute when you leave your textbox and move to another control. Depends on your preferences though.


这篇关于复选框自动检查文本框上的数据为true,不检查为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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