我需要根据国家/地区为表单输入添加前缀,并防止该前缀被编辑。就设置而言,我让它正常工作,但是当我切换国家/地区时,块(只读)没有清除。输入清除但前三列仍然是只读的。我在这里搜索了线程,他们大多都说使用 attr("readonly", false) 语句,但由于某种原因这不起作用。有人请指出问题吗?
<div>
<label for="inputCountry">Country</label>
<div>
<select name="country" id="inputCountry" onchange="SetPhonePrefix();">
<option value="" selected="selected">Please Select</option>
<option value="84">Greece</option>
</select>
</div>
</div>
<script>
function SetPhonePrefix() {
var ctrl = document.getElementById("inputCountry");
var country = ctrl.options[ctrl.selectedIndex].text;
var doLock = false;
if (country == 'Greece') {
document.getElementById('inputFax').value = '+30';
doLock = true;
} else {
document.getElementById('inputFax').value = '';
$("#inputFax").attr("readonly", false);
}
if (doLock) {
var readOnlyLength = $('#inputFax').val().length;
$('#inputFax').on('keypress, keydown', function(event) {
if ((event.which != 37 && (event.which != 39))
&& ((this.selectionStart < readOnlyLength)
|| ((this.selectionStart == readOnlyLength) && (event.which == 8)))) {
return false;
}
});
}
}
</script>