如果文本框 1、文本框 2、文本框 3 中存在值,如何启用单选按钮 1 和文本框 4

How to enable radiobutton1 and textbox4 if values are present in textbox1, textbox2, textbox3

本文关键字:文本 启用 何启用 单选按钮 存在 如果      更新时间:2023-09-26

我在asp页面中有一个表单视图,其中包含4个文本框和一个单选按钮。单击编辑按钮时,如果文本框 1、文本框 2、文本框 3 中存在值,它应该显示单选按钮 1 和文本框 4(即,如果任何一个文本框 (1,2,3) 为空,则不应显示文本框 1 和单选按钮)

如果你使用的是jQuery:

$("#idOfEditButton").live('click', function(){
    if(!$('#idOfTxt1').val() || !$('#idOfTxt2').val() || !$('#idOfTxt3').val()){
        $('#idOfRadio').hide();
        $('#idOfTxt4').hide();   
    }
    else{
        $('#idOfRadio').show();
        $('#idOfTxt4').show();  
    }
});

编辑

您还可以使用类,然后在 if 语句中添加$('.classNameOfAllTxt')(仅一次)。和$('.classfTxt4AndRadio').show(); // or hide.

在表单视图编辑事件中,找到控件并检查文本框是否包含类似文本

TextBox textbox1 = formView.FindControl("TextBox1") 作为 TextBox;

类似地查找文本框 2、文本框 3、文本框 4 和单选按钮 1

然后比较

if(textbox1.Text != string.Empty && textBox2.Text != string.Empty && textBox3.Text != string.Empty)
{
    textbox4.Visible = true;
    Radiobutton1.Visible = true;
}
else
{
    // set visibility to false
}

像下面的事件一样执行此操作

    protected void FormView1_ModeChanged(object sender, EventArgs e)
    {
        if (FormView1.CurrentMode == System.Web.UI.WebControls.FormViewMode.Edit)
        {
            **// Find Controls and Check ConditionHere**
        }
    }

试试吧。希望对您有所帮助。

对于Javascript,请尝试类似以下内容:

 function Check() {
        var b = document.getElementById("<%= FormView1.FindControl("textBox1").ClientID%>");
        var a = document.getElementById("<%= FormView1.FindControl("textBox2").ClientID%>");
        if(a.innerText === "" && b.innerText == "")
        {
              // find the control like above and set visibility to false
              var textbox4 = ....;
              textbox4.visibility = "block"; // attribute for visibility is not verified by me, check to see the correct one if you have problem hidding or showing.
        }
        return false;
    }