问候,我已经成功创建了一个选择字段,其外观与我的HTML页面EXAMPLE的django admin manytomany字段完全相同。但是现在我想从该字段禁用双击功能,我已经尝试过本站点上一个问题的很多方法,并且已经禁用了整个页面的双击功能,但是我仍然可以双击,可以任何人都可以帮助我,谢谢,下面是我的代码:

html:

 <div class="field">
    <select name="settings-user" id="id_settings-user" multiple="multiple" class="selectfilter" data-field-name="User" data-is-stacked="0">
            <option value={{ form.user }}</option>
    </select>
</div>


javascript:

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/static/admin/js/core.js"></script>
<script type="text/javascript" src="/static/admin/js/jquery.init.js"></script>
<script type="text/javascript" src="/static/third_party/jquery/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="/static/admin/js/SelectBox.js"></script>
<script type="text/javascript" src="/static/admin/js/SelectFilter2.js"></script>

<script>
    $(document).ready(function(){
      $("#id_settings-user").dblclick(function(e){
        e.preventDefault();
      });
    });
</script>


页面加载:

<div class="selector-chosen">
    <h2>Chosen User </h2>
    <select id="id_settings-user_to" multiple="multiple" size="0" name="settings-user" class="filtered">
        <option value="1" title="Person A">Person A</option>
        <option value="2" title="Person B">Person B</option>
    </select><a href="#" id="id_settings-user_remove_all_link" class="selector-clearall active">Remove all</a>
</div>

最佳答案

如果使用动态生成的html标签,则应使用document选择器和on()方法,请看以下示例:



$(document).ready(function() {
    $(document).on("dblclick", "*", function(e) {
        console.log("Console log before disable dbclick")
        return false
        console.log("Console log after disable dbclick")
    })
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div class="field">
    <select name="settings-user" id="id_settings-user" multiple="multiple" class="selectfilter" data-field-name="User" data-is-stacked="0">
            <option>Value 1</option>
            <option>Value 2</option>
            <option>Value 3</option>
            <option>Value 4</option>
            <option>Value 5</option>
    </select>
</div>
<div class="selector-chosen">
    <h2>Chosen User </h2>
    <select id="id_settings-user_to" multiple="multiple" size="0" name="settings-user" class="filtered">
        <option value="1" title="Person A">Person A</option>
        <option value="2" title="Person B">Person B</option>
    </select><a href="#" id="id_settings-user_remove_all_link" class="selector-clearall active">Remove all</a>
</div>
</body>

关于javascript - 禁用自动生成的选择标签中的双击,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48185507/

10-16 19:52