如何使文本框只读取一半

时间:2015-04-29 07:20:05

标签: javascript asp.net

我对asp.net中的文本框有疑问。我有一个文本例如:P-70101002-00089-0,我只希望这部分:“70101002”是readonly而另一部分是用户能够编辑的。 模板总是相同的只是数字变化。 有没有机会使用asp.net或任何JavaScript? 谢谢

3 个答案:

答案 0 :(得分:1)

您可以尝试这样:

var x  = $('#myId').val().length;
$('#myId').on('keypress, keydown', function(event) {    
    if ((event.which != 37 && (event.which != 39))
        && ((this.selectionStart < x)
        || ((this.selectionStart == x) && (event.which == 8)))) {
        return false;
    }
});   

<强> JSFIDDLE DEMO

答案 1 :(得分:0)

我们可以使用我们用来获取只读文字的隐藏输入来实现

&#13;
&#13;
function eidturl() {
  var readOnlyLength = $('#page_name_field_hidden').val().length;
  $('#page_name_field').on('keypress, keydown', function(event) {
    if ((event.which != 37 && (event.which != 39)) &&
      ((this.selectionStart < readOnlyLength) ||
        ((this.selectionStart == readOnlyLength) && (event.which == 8)))) {
      return false;
    }
  });
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="page_name_field" type="text" onclick="eidturl()" name="app_name" value="70101002" size="50" />
<input id="page_name_field_hidden" type="hidden" value="70101002" size="50" />
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以尝试使用InputGroup进行Bootstrap

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<label for="basic-url">Your vanity URL</label>
<div class="input-group mb-3">
  <div class="input-group-prepend">
    <span class="input-group-text" id="basic-addon3">P-70101002-</span>
  </div>
  <input type="text" class="form-control" value="00089-0" id="basic-url" aria-describedby="basic-addon3">
</div>