【问题标题】:delimiting multiple select arrays with jquery用jquery分隔多个选择数组
【发布时间】:2013-10-05 16:15:59
【问题描述】:

我正在使用以下代码从四个下拉列表中生成一个简码,其中两个是多选。默认情况下,它用逗号分隔数组,没有空格。我需要将其更改为仅一个空格或逗号和空格。我试过使用 map() 但显然做错了,因为它完全停止了工作。这是具有默认行为的工作脚本:

    function generate_shortcode()
    {
        var level = jQuery('#shortcode_levels').val();
        var username = jQuery('#shortcode_usernames').val();
        var hide = jQuery('#shortcode_hidden').val();
        var logged = jQuery('#shortcode_logged_status').val();
        var shortcode_start = '[myshortcode';
        var shortcode_hide = ' hide="'+hide+'"';
        var shortcode_logged = ' logged="'+logged+'"';          
        var shortcode_level = ' level="'+level+'"';
        var shortcode_username = ' username="'+username+'"';
        var shortcode_end = '] Content Here [/myshortcode]';
        if (hide == '')
            var shortcode_hide = '';                
        if (logged == '')
            var shortcode_logged = '';
        if (level == null)
            var shortcode_level = '';
        if (username == null)
            var shortcode_username = '';
        var shortcode = shortcode_start+shortcode_level+shortcode_username+shortcode_logged+shortcode_hide+shortcode_end;

我需要为代码顶部的前两个变量设置分隔符。

【问题讨论】:

    标签: jquery arrays delimiter multi-select


    【解决方案1】:

    正如jQuery .val() doco 所说:

    “在 <select multiple="multiple"> 元素的情况下,.val() 方法返回一个包含每个选定选项的数组;如果没有选择任何选项,则返回 null。”

    因此,您的变量 levelusername 将分别为数组或 null。无论哪种方式,它们都不是字符串。如果您像当前使用' level="'+level+'"' 那样将数组与字符串连接起来,JavaScript 会将数组转换为逗号分隔的字符串,如您所见 - 请注意,这是标准的 JavaScript 行为,与 jQuery 无关。

    如何解决?如果它们是数组,您可以使用 array .join() method 将它们转换为带有任何分隔符的字符串:

        var level = jQuery('#shortcode_levels').val();
        var username = jQuery('#shortcode_usernames').val();
        var hide = jQuery('#shortcode_hidden').val();
        var logged = jQuery('#shortcode_logged_status').val();
    
        var shortcode_start = '[myshortcode';
        var shortcode_hide = hide === '' ? '' : ' hide="'+hide+'"';
        var shortcode_logged = logged === '' ? '' : ' logged="'+logged+'"';          
        var shortcode_level = level === null ? '' : ' level="'+level.join(", ")+'"';
        var shortcode_username = username === null ? '' : ' username="'+username.join(", ")+'"';
        var shortcode_end = '] Content Here [/myshortcode]';
    
        var shortcode = shortcode_start+shortcode_level+shortcode_username+shortcode_logged+shortcode_hide+shortcode_end;
    

    您可以在上面看到我还用ternary (or conditional) operator 替换了您的每个if 语句:

    var shortcode_level = level === null ? '' : ' level="'+level.join(", ")+'"';
    

    ...这意味着如果level === null 使用'' 否则使用' level="'+level.join(", ")+'"'

    我使用level.join(", ") 来应用逗号和空格作为分隔符,但您可以在其中放置任何内容,因此只使用空格试试level.join(" ")

    【讨论】:

    • 非常非常感谢。谢谢你解释你做了什么。我学了一堆。顺便说一句,工作得很好,除了我必须在记录和隐藏变量上用 '' 替换 null,但非常棒。感激不尽。
    • 不客气。对于您必须更改的部分感到抱歉,我现在已经纠正了一个小小的复制/粘贴错误。
    • 别担心!再次感谢。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 2022-08-02
    • 1970-01-01
    • 2013-06-20
    • 1970-01-01
    • 2019-06-11
    相关资源
    最近更新 更多