简体   繁体   English

JQuery.css(“display”)=“block”无法正常工作

[英]JQuery.css(“display”) = “block” not working

I wanted to show and hide my TextBox based on value selected in RadiobuttonList . 我想根据RadiobuttonList选择的值显示和隐藏我的TextBox I wrote the following code for that 我为此编写了以下代码

$("#<%= rbtnIsPFEnabled.ClientID %>").click(function () {
                pfno = $("#<%= txtPFNo.ClientID %>");
                if ($("#<%= rbtnIsPFEnabled.ClientID %> input:checked").val() == "Yes") {
                    pfno.css("dispay") = "block";
                }
                else
                    pfno.css("dispay") = "none";
            });

Though I had achieved my task by using JQuery.show() and JQuery.hide() but was not satisfied as I wanted to know why first approach failed. 虽然我已经通过使用JQuery.show()JQuery.hide()完成了我的任务但是不满意,因为我想知道为什么第一种方法失败了。 Second is I used $("#<%= rbtnIsPFEnabled.ClientID %>") in above code, can I reduce it to one by using something else second time like this or anything else? 第二个是我用$("#<%= rbtnIsPFEnabled.ClientID %>")在上面的代码,我可以用别的东西第二次像其降低到一个this或别的什么吗?

I tried $(this+" input:checked").val() and $(this.toString()+" input:checked").val() but it did not work, so I had to repeat it. 我试过$(this+" input:checked").val()$(this.toString()+" input:checked").val()但它不起作用,所以我不得不重复它。

$("#id").css("display", "none");
$("#id").css("display", "block");

if Your pfno contains your ID 如果您的pfno包含您的ID

then this should work 这应该工作

$(pfno).css("display", "none");
$(pfno).css("display", "block");

You Should Use FireBug For Debugging 您应该使用FireBug进行调试

EDIT: 编辑:

<script type="text/javascript">
    function fun(obj) {

        if ($("#<%= rbtnIsPFEnabled.ClientID %> input:checked").val()=='Yes') {
            $("#<%= txtPFNo.ClientID %>").css("display", "block");
        }
        else {
            $("#<%= txtPFNo.ClientID %>").css("display", "none");
        }
    }


</script>  

<asp:RadioButtonList   ID="rbtnIsPFEnabled"     runat="server" >
    <asp:ListItem Text="Yes" Value="Yes" onchange="fun(this);"> </asp:ListItem>
    <asp:ListItem Text="No" Value="No" onchange="fun(this);"> </asp:ListItem>
    </asp:RadioButtonList>

    <asp:TextBox runat="server" ID="txtPFNo"/>

You can resolve this issue in multiple way using jQuery . 您可以使用jQuery以多种方式解决此问题。 eg 例如

$(pfno).css("display", "none"); //used when you are defining single css property or in chaining method
$(pfno).css({"display":"none"}); // used when you are defining multiple CSS property           
$(pfno).hide(); //show/hide function also perform display block/none functionality which is mostly used in script

ok first of all this is not correct : 好的,首先这是不正确的:

     pfno.css("dispay") = "block";

above will never work 以上将永远不会奏效

pfno.css({ 'display' : 'block' }); is how you set a css property , 是你如何设置一个CSS属性,

pfno.css( 'display' , 'block' ); is another way to set a css property , so that measn yuo are doing something else wrong.. you need to set an alert or write to the console to confirm that your if statement and your click handler are working 是另一种设置css属性的方法,因此measn yuo正在做其他错误的事情..你需要设置一个警告或写入控制台以确认你的if语句和你的点击处理程序是否正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM