在给定的html代码name_with_initials中,例如mr,mrs,ms,master。从数据库中检索名称时,就像“ MR:Raju”。基于此值,我需要选择前缀名称(MR)和名称中的值(Raju)应该在输入框中。该怎么做?
<div class="input-group">
<span class="input-group-addon">
<select name="name_with_initials" id="name_with_initials">
<option value="MR">MR</option>
<option value="MRS">MRS</option>
<option value="MS">MS</option>
<option value="MASTER">MASTER</option>
</select>
</span>
<input type="text" name="name_with_initials" id="customername" onKeyPress="return ValidateAlpha(event);" class="form-control detailinfo" placeholder="Full Name">
</div><!-- /input-group -->
答案 0 :(得分:2)
您可以使用冒号(:)分隔首字母缩写和名称,并将值设置为选择下拉列表和输入文本框:
var dbValue = 'MR:Raju';
var initials = dbValue.split(':')[0];
var username = dbValue.split(':')[1];
$('#name_with_initials').val(initials);
$('#customername').val(username);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="input-group">
<span class="input-group-addon">
<select name="name_with_initials" id="name_with_initials">
<option value="MR">MR</option>
<option value="MRS">MRS</option>
<option value="MS">MS</option>
<option value="MASTER">MASTER</option>
</select>
</span>
<input type="text" name="name_with_initials" id="customername" onKeyPress="return ValidateAlpha(event);" class="form-control detailinfo" placeholder="Full Name">
</div>