禁用在自动生成的选择标记中双击

时间:2018-01-10 10:40:14

标签: javascript jquery html django

问候,我已经成功创建了一个与我的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>

1 个答案:

答案 0 :(得分:1)

如果您正在使用动态生成的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>