There is a JS code that allows opening and closing access to the input field, depending on the value of the checkbox :

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> </head> <body> <form class="form-inline"> <div class="checkbox col-md-offset-5"> <label> <input id="check1" type="checkbox" onchange="checkk(this);"/> </label> </div> <div class="form-group"> <label class="sr-only" for="exampleInputEmail3">Email address</label> <input type="text" class="form-control" id="text1" disabled="true"> </div> </form> </body> </html> <script> function checkk(elem) { var value = elem.checked; if(value == true) { text1.disabled = false; } else { text1.disabled = true; text1.value = ' '; } } </script> 

enter image description here

I'm trying to do the same in my ASP.NET MVC project.

AddLearner.cshtml

 ... <td align="center"> <input type="checkbox" value="checked" onchange="checkk(this);" /> </td> <td align="center"> @Html.TextBoxFor(x => x.ElementPlans[i].ValueOge15, new { id = String.Format("text{0}", number++), disabled = false }) </td> </tr> } </table> } <script> function checkk(elem) { var value = elem.checked; if (value == true) { text1.disabled = false; } else { text1.disabled = true; } } </script> 

But my text1 field is independent of how the values ​​of the disabled attribute are spelled

 disabled = false disabled = true disabled = "" @disabled = false @disabled = true или @disabled = "" 

always remains unavailable

enter image description here

Why it happens?

  • one
    You create several elements with the same id - text1. If you add another input (or any other element) with id = "text1" to the original version, then nothing will work either. - Yaant
  • @Yaant, well, it will be, but only depends on the browser - how exactly :-) and by the way the id is different - Grundy
  • @Grundy They became different after my comment, see the history of edits. :) As for dependence on the browser, I agree, when writing a comment, I checked it only in Chrome. :) - Yaant

1 answer 1

I assume that your error occurs in the js script, because it cannot find the text1 object. It should be like document.getElementbyId ("text1"). Style.disabled = false