How to enable/disable inputs in blazor

前端 未结 3 1037
盖世英雄少女心
盖世英雄少女心 2021-02-18 13:03

I am trying to Enable/Disable a group of time inputs in Blazor based on a checkbox ; while for inputs of type button

相关标签:
3条回答
  • 2021-02-18 13:25

    To disable elements you should use the disabled attribute.

    I've modified your code a bit and this will do what you're after. Blazor will automatically add or remove the disabled attribute based on the IsDisabled value.

    You should use the disabled attribute on your button as well. It's a much better practice.

    <button type="button" disabled="@IsDisabled"></button>
    <input bind="@IsDisabled" type="checkbox" />
    
    <input disabled="@IsDisabled" type="time" />
    
    @code{
        protected bool IsDisabled { get; set; }
    }
    

    You can still combine this with applying a CSS class for styling the disabled element. It's up to you.

    0 讨论(0)
  • 2021-02-18 13:30

    There is an alternative way you can achieve this.

    <fieldset disabled=@ShouldBeDisabled>
      Your input controls in here will be disabled/enabled by the browser
    </fieldset>
    
    0 讨论(0)
  • 2021-02-18 13:38

    You can also get the value to disable the button directly as an expression

    <input disabled="@(MyCondition ? true : false)" type="checkbox" />     
    
    0 讨论(0)
提交回复
热议问题