问题
I have a form with a textbox that has an attribute of read-only. This isn't a big deal but I thought I would ask, so is there a way to make that textbox so that the user cannot select it and put their cursor in it?
The disabled attribute does that, but then that textbox doesn't get submitted with the form.
Is there a js/jquery solution for this?
SNIPPET
<form>
<div class="col-md-9">
@Html.EditorFor(model => model.Time, new { htmlAttributes = new { @class = "form-control clear-textbox create-form-txtbox time-txtbox", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Time, "", new { @class = "text-danger" })
</div>
<input type="submit" value="Submit" />
</form>
Any help is appreciated.
回答1:
Try this. Its working for me. Replace @Html.EditorFor with this control.
<input type="text" value="@Model.Time" id="Time" name="Time" class="form-control" readonly />
回答2:
Use inline onkeydown="return false;"
attribute
or if you want a javascript solution
$('#someId').keydown(function(e) {
e.preventDefault();
return false;
});
回答3:
I have used Stephen's comment as an answer and it works.
I have created a hidden input field to submit to the server, but the user will only see the disabled textbox with the value that that hidden input field is holding.
来源:https://stackoverflow.com/questions/43634491/make-readonly-textbox-non-selectable