Home > Net >  How to set an input element as disabled
How to set an input element as disabled

Time:10-18

<input type="radio" @(Model.IsradACHDisabled ? "disabled" : "") asp-for="SelectedPaymentType" id="radACH" value="ACH" />ACH<br />

For the above code the disable attribute works but the SelectedPaymentType value is lost on a POST

<input type="radio" asp-for="SelectedPaymentType" id="radACH" value="ACH" />ACH<br />

With the above code SelectedPaymentType I can get back on a POST but how do I set the disabled attribute

CodePudding user response:

It just performed as excepted,if you set the inputbox disabled,when you submit the form,the value won't be submitted, You could try with readonly attribute instead,also,you could modify the property when you submit the form:

<form id="form" method="post" asp-controller="Home" asp-action="Post">
    <input  name="PostId">
    
    <input id="someinput" disabled="disabled" name="Description" value="des">
    <div >
        <input type="submit" value="Submit" />
    </div>
</form>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script>

    $('#form').submit(function() {
        $("#someinput").prop('disabled', false);
    
    })
</script>

CodePudding user response:

You may use a Hidden Input to hold that value for post back purpose while remain the existing Input as disabled.

If your POST if for database update, it is recommend do not use the SelectedPaymentType value from POST. This can lead to security hole, other can still manipulate this value.

CodePudding user response:

Try to use :

<input type="radio" @Model.IsradACHDisabled ? "disabled" : "" asp-for="SelectedPaymentType" id="radACH" value="ACH" />ACH1<br />

or:

@if(Model.IsradACHDisabled=="disabled")
{
 <input type="radio" disabled asp-for="SelectedPaymentType" id="radACH" value="ACH" />ACH1<br />
}
else{
   <input type="radio"  asp-for="SelectedPaymentType" id="radACH" value="ACH" />ACH1<br />
}
  • Related