开发者

convert `[object Object]` and append it in a class.?

开发者 https://www.devze.com 2023-04-02 09:32 出处:网络
Q1. How can convert [object Object] to word? LIKE: [object Object] =to=> salam Q2. How can append outputs result search[after click on it] in class .list with(mid) tow input.

Q1. How can convert [object Object] to word?

LIKE: [object Object] =to=> salam

Q2. How can append outputs result search[after click on it] in class .list with(mid) tow input.

LIKE: If resulet search was value salam and after click on it get as:

EXAMPLE:

[object Object] -> input ... & input ...    
[object Object] -> input ... & input ...
[object Object] -> input ... & input ...

Js:

$('.auto_complete').keyup(functio开发者_开发百科n () {
    var dataObj = $(this).closest('form').serialize();
    $.ajax({
        type: "POST",
        dataType: 'json',
        //url: 'http://binboy.gigfa.com/admin/tour_foreign/auto_complete',
        url: 'auto_complete',
        data: dataObj,
        cache: false,
        success: function (data) {
            var id_name = $('.list_autobox_hotel').attr('id');
            $('.list_autobox_hotel').show().html('');
            if (data == 0) {
                $('.list_autobox_hotel').show().html('<p><b>there is no</b></p>');
            } else {
                $.each(data, function (index, value) {
                    $('<p id="' + value.name + '">' + value.name + '</p>').appendTo('.list_autobox_hotel');
                });
                //////////////////////*HERE Q1//////////////////////
                $('.list_autobox_hotel p').bind("click", function (e) {
                    e.preventDefault();
                    var ac = $(this).attr('id');
                    var ok = $.grep(data, function (e) {
                        return e.name == ac;
                    })[0].units;
                    //alert(ok);
                    /////////////*HERE Q2//// for append////////
                    $(ok).appendTo('.list');

                    /////////////HERE Q2*////////////
                    $(this).remove();
                    return false;
                });

                //////////////////////HERE Q1*//////////////////////
                $('body').click(function () {
                    $(".list_autobox_hotel p").hide().remove();
                    $('.auto_complete').val('');
                    $('.list_autobox_hotel').show().html('');
                    $('.list_autobox_hotel').css('display', 'none');
                });

            }
        },
        "error": function (x, y, z) {
            // callback to run if an error occurs
            alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
        }
    });
});

PHP[insert to database]:

  $units = array();
  $name_units = $this->input->post('name_units');
  $price_units = $this->input->post('price_units');
  $checkbox_units = $this->input->post('checkbox_units');
  foreach ($name_units as $idx=>$name){
      $units[] = array(
    'name_units' => $name_units[$idx],
    'price_units' => $price_units[$idx],
    'checkbox_units' => $checkbox_units[$idx],
      );
  }
 $data = array('name' =>$this -> input -> post('name'),              
               'units' => json_encode($units)
 );

      $this -> db -> insert('hotel_submits', $data);

PHP[select code]:

$data = array();
foreach ($query_hotel_search->result() as $row)
{
   $units = json_decode($row->units);
   $data[] = array('name' => $row->name, 'units' =>$units );
}
echo json_encode($data);

DEMO: There are here the contents of the database and output code PHP and test code for you => CLICK HERE

UPDATE: json response:

Output values salam & salavat of php code: [this values select(selcet * from ...) by json_encode()]

salam:

[{"name":"salam","units":[{"name_units":"salam","price_units":"74,554","checkbox_units":["minibar","mobleman"]},{"name_units":"mokhles","price_units":"4,851,269","checkbox_units":["mobleman","tv"]},{"name_units":"fadat","price_units":"85,642","checkbox_units":["minibar","mobleman","tv"]}]}]

salavat:

[{"name":"salavat","units":[{"name_units":"salam","price_units":"5,452","checkbox_units":null},{"name_units":"khobe","price_units":"5,452,545","checkbox_units":["minibar","mobleman"]}]}]


my answer is totally based on assumption you are getting this json as response when i searched "salam"

[
    {
        "name": "salam",
        "units": [
            {
                "name_units": "salam",
                "price_units": "74,554",
                "checkbox_units": [
                    "minibar",
                    "mobleman"
                ]
            },
            {
                "name_units": "mokhles",
                "price_units": "4,851,269",
                "checkbox_units": [
                    "mobleman",
                    "tv"
                ]
            },
            {
                "name_units": "fadat",
                "price_units": "85,642",
                "checkbox_units": [
                    "minibar",
                    "mobleman",
                    "tv"
                ]
            }
        ]
    }
]

in the success callback iterate over the json as follow

if (data == 0) {
$('.list_autobox_hotel').show().html('<p><b>there is no</b></p>');
 }
 else
 {
  $.each(data[0].units, function (index, value) {
   $('<p id="' + data[0].units[index].name_units + '">' + data[0].units[index].name_units + '</p>').appendTo('.list_autobox_hotel');
  });

here is a demo hopefully you will get the idea http://jsfiddle.net/BBSyy/

EDIT

see this fiddle http://jsfiddle.net/BBSyy/1/

Another Edit

see this fiddle with grep example http://jsfiddle.net/BBSyy/3/

0

精彩评论

暂无评论...
验证码 换一张
取 消