Make ReadOnly Textbox Non-Selectable

大城市里の小女人 提交于 2019-12-13 09:00:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!