How to get asp.net Session value in jquery method?

前端 未结 6 1475
迷失自我
迷失自我 2020-12-13 16:11

I want to access a Session value in jquery method in the ASP.NET MVC view page. See the below code,

$(\'input[type=text],select,input[type=checkbox],input[ty         


        
6条回答
  •  遥遥无期
    2020-12-13 16:53

    Many comments:

    1. You cannot "Access Session from jQuery". You use MVC and asp.net to create an HTML page (with JavaScript). Session is a server-side object, and JavaScript runs on the client side.
    2. Take a look at jQuery's selectors. They have useful selectors like :checkbox, :text, etc.
    3. Your code produce the JavaScript you expect: it compiles, runs, and produces JavaScript with true or false on the right place.
    4. This is not the way to disable an element. If an element has the 'disabled' attribute it will be disabled, no matter the value. is also a disabled check box, so your controls are always disabled.
    5. If that is the way you choose anyway, consider:

      var isCoBrowse = <%= Session["Name"].ToString().ToLower() %>;
      if(!isCoBrowse) //disable controls
        $(":text,:checkbox,:radio").attr("disabled","disabled"); //standard.
      

      This will produce the client-side JavaScript code:

      var isCoBrowse = true;
      

      And, to enable an element:

      $("input").removeAttr("disabled");
      

    Also, there are much better ways to accomplish this. Have you considered disabling the controls on server side, if possible?

提交回复
热议问题