如何在HTML中激活JavaScript函数 [英] How to make javascript function active in html

查看:56
本文介绍了如何在HTML中激活JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码来自 http://jsfiddle.net/ejqngpn5/,但我显然将javascript和html部分错误地合并在一起,或者因为它不能像在线方式那样工作而丢失了其他内容.如何将javascript插入并在html代码中调用函数?我在脚本中使用了AJAX调用,但不知道使这两个代码片段一起工作.它应在下拉菜单中产生onClick多个复选框列表.非常感谢.

This code came from http://jsfiddle.net/ejqngpn5/ but I apparently am putting the javascript and html portions together incorrectly or am missing something else because it is not working as it did on-line. How is the javascript inserted into and the function invoked in html code? I use AJAX calls in my scripts but don't know to make these two code fragments work together. It should produce, onClick, a multiple check-box list in a drop down menu. Many thanks.

HTML:

<body>
    <select size="5" name="lstStates" multiple="multiple" id="lstStates">
        <option value="NJ">New Jersey</option>
        <option value="NY">New York</option>
        <option value="OH">Ohio</option>
        <option value="TX">Texas</option>
        <option value="PA">Pennsylvania</option>
    </select>   
</body>

JAVASCRIPT

JAVASCRIPT

$(function () { 
    $('#lstStates').multiselect({ 
        buttonText: function(options, select) {
            console.log(select[0].length);
            if (options.length === 0) {
                return 'None selected';
            }
            if (options.length === select[0].length) {
                return 'All selected ('+select[0].length+')';
            }
            else if (options.length >= 4) {
                return options.length + ' selected';
            }
            else {
                var labels = [];
                console.log(options);
                options.each(function() {
                    labels.push($(this).val());
                });
                return labels.join(', ') + '';
            }
        }

    });
});

推荐答案

您应将以下资源包括到项目中:jquery,bootstrap,bootstrap-multiselect.

You should include these resources to your project: jquery, bootstrap, bootstrap-multiselect.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css">
<script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js"></script>

这篇关于如何在HTML中激活JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆