【问题标题】:JavaScript function only selects first checkbox when told to pre-select moreJavaScript 函数仅在被告知要预选更多时才选择第一个复选框
【发布时间】:2011-04-09 11:52:51
【问题描述】:

我有一个 JavaScript 函数,当从选择框中选择某个值时,它会动态创建复选框元素。为了验证复选框是否被选中,我从 PHP 发送一个变量。

例如:var_id=1,2,3,5。我使用jQuery.inArray,但我只选中了五个列表中的第一个复选框。

这是创建复选框列表的函数:

function outcheckboxes(_id, _text, var_id) {
  (function($){
    var topicContainer = jQuery('#classificationss_id');

    topicContainer.append(
        $('<li />')
        .addClass('checkboxclassid')
        .append(
            $('<input />', {
            id:      'classid' + _id,
            name:    'classid[]',
            value:   _id,
            type:    'checkbox',
            checked: !$.inArray(_id, var_id)
            })                                      
        )
        .append(
            $('<label />', {
            'for':  'classid' + _id
            }).text( _text)
            )
        )
    })(jQuery)
}

我从另一个函数调用函数outcheckboxes

function Classifications(obj_parent, obj_this, obj_child, var_id) {

        var catalog_id = jQuery("#"+obj_parent).selectedValues()[0];
        jQuery("#classificationss_id").children().remove();
        jQuery("#"+obj_this).children().remove();
        jQuery("#"+obj_child).children().remove();


        if(catalog_id>0){
            jQuery.ajax({
                type : "GET",
                url : "../index.php",
                data : "option=com_namecat&view=lists&format=raw&list=classifications&id=" + catalog_id,
                dataType : "xml",
                success : function(xml) {
                    var _id = "";
                    var _text = "";

                    jQuery(xml).find("row").each( function() {
                        _id = jQuery(this).find("id").text();
                        _text = jQuery(this).find("text").text();
                        jQuery("#"+obj_this).addOption(_id, _text );

                        outcheckboxes(_id,_text,var_id);

                    });
                }
            });
        }
}

我哪里错了?

【问题讨论】:

  • 想必你知道$的简写吗?
  • 试过alert()-ing var_id.join(', ')的值?
  • 使用 $ 速记我在浏览器中有错误。我在 Joomla 框架中使用 jquery,并且只有 jQuery 才能正常工作。我得到var_id.join is not a function
  • 那么var_id 是什么?我以为是Array,它有内置函数join
  • var_id 是来自数据库的字符串。这是由 , 分隔的 id-s 列表。我从 joomla 发送 var_id 和事件 onchange。这是来自 php &lt;?php echo JHTML::_('Select.genericlist',$this-&gt;catalogs,"catalog_id",'onchange="updateClassifications(\'catalog_id\',\'classification_id\',1, \''.$this-&gt;data-&gt;classification_id.'\')"',"catalog_id","catalog_name", $this-&gt;data-&gt;catalog_id); ?&gt; 的代码

标签: php javascript jquery


【解决方案1】:

首先,您应该这样编写代码:

编辑:

添加了速记

(function($) {
    $('<li />')
        .addClass('checkboxclassid')
        .append(
            $('<input />', {
                id:      'classid' + _id,
                name:    'classtype[]',
                value:   _id,
                type:    'checkbox',
                checked: !$.inArray(_id, var_id) 
            })                                      
        )
        .append(
            $('<label />', {
                'for':  'classid' + _id
            }).text( _text)
        )
})(jQuery)

【讨论】:

  • 使用$ 速记我在浏览器中有错误。我在 Joomla 框架中使用 jquery,并且只有 jQuery 工作正常。
  • @Alexandru:我已经修改了代码,以便 $ 简写可以与 joomla 一起使用。
  • 谢谢埃里克。我试过修改代码,但结果是一样的。也许问题出在var_id。带有警报 a 可以看到例如4,5,10。因此,ID为4510的复选框必须被选中,但我只选中了ID为4的复选框。
猜你喜欢
  • 2016-08-05
  • 1970-01-01
  • 2018-04-01
  • 2011-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多