从文本框中切换大小写接受值

时间:2018-08-03 07:43:54

标签: javascript html

public class BlockedHandler : AuthorizationHandler<BlockedRequirement>
{
    protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, BlockedRequirement requirement)
    {
        if (!context.User.HasClaim(c => c.Type == UserClaimTypes.BlockedFrom))
        {
            context.Succeed(requirement);
            return Task.CompletedTask;
        }

        // User is blocked!

        if (context.Resource is AuthorizationFilterContext mvcContext)
        {
            if (mvcContext.ActionDescriptor is ControllerActionDescriptor descriptor)
            {
                var allowBlocked = descriptor.ControllerTypeInfo.CustomAttributes
                    .Concat<CustomAttributeData>(descriptor.MethodInfo.CustomAttributes)
                    .Any(x => x.AttributeType == typeof(AllowBlockedAttribute));

                // User can access called action.
                //
                if (allowBlocked)
                    context.Succeed(requirement);
            }

            // Ugly to call this as the next step?
            // mvcContext.HttpContext.Request.Path = GenericPaths.Blocked;
        }

        // Prevent redirection to AccessDenied
        // Stop authorization chain.

        return Task.CompletedTask;
    }
}

从文本框中接受值并在switch中匹配大小写并在这种情况下打印文本吗?

2 个答案:

答案 0 :(得分:0)

不知道为什么在这里使用document.write,有更好的方法向页面添加元素,这似乎也导致函数无法执行。

您还需要在var sb = document.getElementById('a').value;函数内移动casss()行,因为否则它将仅在第一次创建文本框时读取一次文本框的值,而您想每次获取最新的值按下按钮的时间。

演示(使用静态标记代替document.write):

    //switch

    function casss() {
      var sb = document.getElementById('a').value;
      console.log(sb);
      switch (sb) {
        case 1:
          sb = "php";
          break;
        case 2:
          sb = "html";
          break;
        case 3:
          sb = "css";
          break;
        case 4:
          sb = "javascript";
          break;
      }
      document.getElementById("result").innerText = sb;
    }
<!Doctype html>
<html>

<body>
<br><input type='text' id='a'> <button onclick='casss()'>Show Value</button>
  <p id="a"></p>
  <p id="result"></p>
</body>

</html>

答案 1 :(得分:0)

您的代码中有两个问题:

  1. 已经有一个<p>元素和一个id='a'。删除该ID或更改其ID以使其唯一。
  2. 您的switch案例期望在case条件下使用整数值,因此请在parseInt(sb)参数内使用switch

    //switch
    var sb;
    document.write("<br><input type='text' id='a'> <button onclick='casss()'>Show Value</button>");
    function casss() {
      sb = document.getElementById('a').value;
      switch (parseInt(sb)) {
        case 1:
          sb = "php";
          break;
        case 2:
          sb = "html";
          break;
        case 3:
          sb = "css";
          break;
        case 4:
          sb = "javascript";
          break;
      }
      document.write("<br>" + sb);
    }