Jquery 页面加载时如何锁定/只读引导标记输入字段?
我正在使用。这是工作,因为它应该 但是,当页面加载时,我希望bootstrap tagsinput字段是只读的(即被锁定且无法输入文本) 我编写了一些代码,当选中/取消选中布尔复选框时,可以锁定/解锁引导标记输入字段 此代码正在运行。这是:Jquery 页面加载时如何锁定/只读引导标记输入字段?,jquery,twitter-bootstrap,tags,bootstrap-tags-input,Jquery,Twitter Bootstrap,Tags,Bootstrap Tags Input,我正在使用。这是工作,因为它应该 但是,当页面加载时,我希望bootstrap tagsinput字段是只读的(即被锁定且无法输入文本) 我编写了一些代码,当选中/取消选中布尔复选框时,可以锁定/解锁引导标记输入字段 此代码正在运行。这是: <script type="text/javascript"> $(document).ready(function() { // lock the tags input field. $("#id_boo
<script type="text/javascript">
$(document).ready(function() {
// lock the tags input field.
$("#id_bootstrap_tagsinput_tag_input").attr('readonly', true);
// when the user changes the search engine ressults option, lock / unlock the tags input..
$('#id_resume_published_details_search_engine_results').change(function() {
if($(this).is(":checked")) {
// unlock the tags input field.
$("#id_bootstrap_tagsinput_tag_input").attr('readonly', false);
} else {
// remove all tags before locking the input field.
$('#id_bootstrap_tagsinput_tag_wrapper').tagsinput('removeAll');
// lock the tags input field.
$("#id_bootstrap_tagsinput_tag_input").attr('readonly', true);
}
});
});
</script>
$(文档).ready(函数(){
//锁定标记输入字段。
$(“#id#u bootstrap_tagsinput_tag_input”).attr('readonly',true);
//当用户更改搜索引擎ressults选项时,锁定/解锁标签输入。。
$(“#id#U resume_published_details_search_engine_results”)。更改(函数(){
如果($(this).is(“:checked”)){
//解锁标记输入字段。
$(“#id#u bootstrap_tagsinput_tag_input”).attr('readonly',false);
}否则{
//锁定输入字段之前,请删除所有标记。
$('id#u bootstrap_tagsinput_tag_wrapper')。tagsinput('removeAll');
//锁定标记输入字段。
$(“#id#u bootstrap_tagsinput_tag_input”).attr('readonly',true);
}
});
});
但是,当页面最初加载时,引导标记输入字段不是锁定/只读的
有人能提出为什么页面加载时引导标记输入字段不是只读/锁定的吗
谢谢
[编辑]
我尝试过使用
$(“#id_bootstrap_tagsinput_tag_input”).attr('disabled','disabled')代码>但这没有效果。这似乎总是对我最有效:
$("#id_bootstrap_tagsinput_tag_input").prop('disabled', true);
当然,反过来重新启用:
$("#id_bootstrap_tagsinput_tag_input").prop('disabled', false);
页面加载完成后,bootstrap tagsinput字段将加载
因此,我延迟了将readonly应用于bootstrap标记输入字段
我的代码如下
我希望这个答案对某人有所帮助
<script type="text/javascript">
$(document).ready(function() {
// lock the tags input field.
// run this function from the setTimeout function below with a delay of 1 second.
// the bootstrap input tag field loads after the page has loaded
function delayInputDisable() {
$("#id_bootstrap_tagsinput_tag_input").attr('readonly', true);
}
setTimeout(delayInputDisable, 1000); // use setTimeout() to execute.
// when the user changes the search engine ressults option, lock / unlock the tags input..
$('#id_resume_published_details_search_engine_results').change(function() {
if($(this).is(":checked")) {
// unlock the tags input field.
$("#id_bootstrap_tagsinput_tag_input").attr('readonly', false);
} else {
// remove all tags before locking the input field.
$('#id_bootstrap_tagsinput_tag_wrapper').tagsinput('removeAll');
// lock the tags input field.
$("#id_bootstrap_tagsinput_tag_input").attr('readonly', true);
}
});
});
$(文档).ready(函数(){
//锁定标记输入字段。
//从下面的setTimeout函数运行此函数,延迟1秒。
//引导输入标记字段在页面加载后加载
函数delayInputDisable(){
$(“#id#u bootstrap_tagsinput_tag_input”).attr('readonly',true);
}
setTimeout(delayInputDisable,1000);//使用setTimeout()执行。
//当用户更改搜索引擎ressults选项时,锁定/解锁标签输入。。
$(“#id#U resume_published_details_search_engine_results”)。更改(函数(){
如果($(this).is(“:checked”)){
//解锁标记输入字段。
$(“#id#u bootstrap_tagsinput_tag_input”).attr('readonly',false);
}否则{
//锁定输入字段之前,请删除所有标记。
$('id#u bootstrap_tagsinput_tag_wrapper')。tagsinput('removeAll');
//锁定标记输入字段。
$(“#id#u bootstrap_tagsinput_tag_input”).attr('readonly',true);
}
});
});
谢谢,但你的建议无效。问题可能是在页面加载完成后加载引导标记SINPUT?这是可能的。是您的$(document).ready(function(){});在html的末尾或开头。如果还没有,我建议把它移到最后。谢谢。移动$(document.ready(function(){});不起作用。如果不查看该页的整个代码库,我不知道还能告诉您什么。我使用了1秒的setTimeout
延迟,这就成功了。上面的代码回答。