【问题标题】:How to enable radiobutton1 and textbox4 if values are present in textbox1, textbox2, textbox3如果 textbox1、textbox2、textbox3 中存在值,如何启用 radiobutton1 和 textbox4
【发布时间】:2012-07-05 17:47:59
【问题描述】:

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

【问题讨论】:

  • 检查我的答案。如果不想用jQuery的话,DnshPly9的回答也很好。

标签: c# javascript asp.net asp.net-mvc asp.net-mvc-2


【解决方案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

【讨论】:

    【解决方案2】:

    在formview编辑事件中,找到控件并检查文本框是否包含类似的文本

    TextBox textbox1 = formView.FindControl("TextBox1") as TextBox;

    类似地找到 TextBox2、TextBox3、TextBox4 和 Radiobutton1

    然后比较

    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;
    
        }
    

    【讨论】:

    • 对不起,你是对的,但你可以在ModeChanged事件中试试???像 FormView1.CurrentMode == System.Web.UI.WebControls.FormViewMode.Edit 之类的东西然后做...... Javascript!是的,这是可能的,但我必须先尝试一下
    • 抱歉,我测试了我的 javascript 代码,但没有测试服务器端代码,请尝试使用 FormViewDiagnostic PreRender 事件,如链接所示 forums.asp.net/post/2560651.aspx
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多