【问题标题】:How could I convert jquery object to string? [duplicate]如何将 jquery 对象转换为字符串? [复制]
【发布时间】:2013-07-01 14:04:48
【问题描述】:

addFormData() 函数中的“dt”值:

[Object {'name':'val1', 'value': 'ww'}]

但我想转换为字符串并删除 '[Object' 和 ']' 部分:

{'name':'val1', 'value': 'ww'}

然后添加到“参数”:

$("#flex1").flexOptions({params: [{'name':'options', 'value': '<?php echo($flexiOptions); ?>'},{'name':'model', 'value': 'Product'},{'name':'val1', 'value': 'ww'}]});

我的完整代码:

<?php
$flexiOptions = serialize(
                            array(
                                'fields' => array('Product.id', 'Coupon.id', 'Order.id', 'Order.order_code', 'Product2.name', 'Coupon.code', 'Coupon.date_add', 'Coupon.status'),
                                'conditions' => array('Product.shop_id ='.$shopId),
                                'joins' => array(
                                            array('table' => $prefix.'order_items',
                                                    'alias' => 'OrderItem',
                                                    'type' => 'inner',
                                                    'conditions' => array('Product.id = OrderItem.product_id')
                                                    ),
                                            array('table' => $prefix.'products',
                                                    'alias' => 'Product2',
                                                    'type' => 'inner',
                                                    'conditions' => array('Product.id = Product2.id')
                                                    ),
                                            array('table' => $prefix.'coupons',
                                                    'alias' => 'Coupon',
                                                    'type' => 'inner',
                                                    'conditions' => array(
                                                                        'OrderItem.id = Coupon.order_item_id',
                                                                        'Coupon.shop_id ='.$shopId
                                                                        )
                                                    ),
                                            array('table' => $prefix.'orders',
                                                    'alias' => 'Order',
                                                    'type' => 'inner',
                                                    'conditions' => array('Order.id = OrderItem.order_id')
                                                    ),
                                                )
                                )
                        );

$flexigrid_settings = "
                    //params: 
                        //[
                        //{'name':'options', 'value': '".$flexiOptions."'},
                        //{'name':'model', 'value': 'Product'}
                        //],
                    colModel :
                        [
                        {display: 'ID', name : 'Coupon.id', sortable : true, align: 'left'},
                        {display: 'Rendelés kód', name : 'Order.order_code', sortable : true, align: 'left'},
                        {display: 'Rendelés megnyitása', name : 'Order.id', sortable : true, align: 'left', process:flexOrderOpen},
                        {display: 'Tétel elnevezése', name : 'Product2.name', sortable : true, align: 'left'},
                        {display: 'Kupon kód', name : 'Coupon.code', sortable : true, align: 'left'},
                        {display: 'Létrehozva', name : 'Coupon.date_add', sortable : true, align: 'left'},
                        {display: 'Státus', name : 'Coupon.status', sortable : true, align: 'left', process:flexStatus},
                        ],
                    searchitems :
                        [
                        {display: 'Tétel elnevezése', name : 'Product2.name', isdefault: true}
                        ],
                    ";
?>
<form id="flexiSform">
Kuponkód: <input type="text" name="val1" value="ww">
</form>
<table id="flex1" style="width:auto;"></table>
<script type="text/javascript">

function flexOrderOpen( celDiv, id) {
    $( celDiv ).ready( function() {
        $( celDiv ).html('<a href="/admin/orders/open/'+$( celDiv ).html()+'">rendelés megnyitása</a>');
    });
    }

function flexStatus( celDiv, id) {
    $( celDiv ).ready( function() {
        if ($(celDiv).html() == 0)
            {
            $(celDiv).html('Beváltatlan');
            }
        else
            {
            $(celDiv).html('Beváltott');
            }
    });
    }

$("#flex1").flexigrid({
            url: '/flexi_grids/listsjson/',
            <?php print($flexigrid_settings); ?>
            title: 'Kuponok listája',
            onSubmit: addFormData,
        });

function addFormData(){
    var dt = $('#flexiSform').serialize();
    $("#flex1").flexOptions({params: [{'name':'options', 'value': '<?php echo($flexiOptions); ?>'},{'name':'model', 'value': 'Product'},dt]});
    return true;
}

$('#flexiSform').submit(function (){
    $('#flex1').flexOptions({newp: 1}).flexReload();
    return false;
});
</script>

【问题讨论】:

    标签: javascript jquery cakephp flexigrid


    【解决方案1】:

    您显示的输出似乎不是来自.serialize,因为.serialize 返回的是字符串。

    在任何情况下,您都不想将任何内容转换为字符串,似乎您想将对象添加到params 数组。改用.serializeArray 并将dt[0] 添加到数组中:

    function addFormData(){
        var dt = $('#flexiSform').serializeArray();
        $("#flex1").flexOptions({params: [
            {'name':'options', 'value': '<?php echo($flexiOptions); ?>'},
            {'name':'model', 'value': 'Product'},
            dt[0]
        ]});
        return true;
    }
    

    如果要添加表单中的所有值,或者连接两个数组:

    $("#flex1").flexOptions({params: [
        {'name':'options', 'value': '<?php echo($flexiOptions); ?>'},
        {'name':'model', 'value': 'Product'}
    ].concat(dt)});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 2011-02-02
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      相关资源
      最近更新 更多