How to lock/readonly bootstrap-tagsinput field when page loads?

问题: I am using bootstrap-tagsinput. This is working as it should. However, when the page loads, I want the bootstrap-tagsinput field to be readonly (that is locked and not ab...

问题:

I am using bootstrap-tagsinput. This is working as it should.

However, when the page loads, I want the bootstrap-tagsinput field to be readonly (that is locked and not able to enter text).

I have written some code that will allow the bootstrap-tagsinput field to be locked/unlocked when a boolean check box is checked/unchecked.

This code is working. Here it is:

<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>

However, when the page initially loads, the bootstrap-tagsinput field is not locked/readonly.

Can anyone suggest why the bootstrap-tagsinput field is not readonly/locked when the page loads?

Thanks.

[EDIT]

I have tried using $("#id_bootstrap_tagsinput_tag_input").attr('disabled', 'disabled'); but this has no effect.


回答1:

It appears the that bootstrap-tagsinput field loads after the page load is completed.

So I placed a delay on applying the readonly to the bootstrap-tagsinput field.

My code is below.

I hope this answer helps someone.

<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);
        }
    });
});


回答2:

This is what always seems to work best for me:

$("#id_bootstrap_tagsinput_tag_input").prop('disabled', true);

And of course, the reverse to re-enable:

$("#id_bootstrap_tagsinput_tag_input").prop('disabled', false);
  • 发表于 2019-01-11 14:48
  • 阅读 ( 639 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除