简体   繁体   中英

Get value of parameter in Javascript JSON string?

I have a click handler on a jQuery datatables table row - if you click a row a new dialog is launched. However i am having soem problems getting the ID which I need to pass to my ajax function which launches the pop up.

I have the line of code below:

var rowData = carTable.fnGetData(event.target.parentNode);

If I then do the following.

var json = JSON.stringify(rowData);

If I then alert(json); I get the following pop up

{"ExtensionData":{}, "CarRegNo" : "ABC 123", 
"CarNumber": "98765", "CarID" : 1234, 
"CarName" : "BMW", "CarFaults" : 2, 
"CarDealerID" : 16, "DealerName" : "WeSellCars"}

The only value I need is the 1234 from CarID? How can I easily get this value?

I tried the following so far with no luck - so without doing the JSON.stringify I just got the key from rowData - when I just alerted the key I could see all the different values, ie CarID, CarRegNo - however then I tried to alret the actual value of CarID with the code below - but no response comes back - the browser just seems to hang.

        for (var key in rowData) {
            alert(key);
            if (key == 'CarID')
                alert(rowData[0][key]);
        }

Should literally be just:

var carId = rowData.CarId;

The rowData variable already contains the single object, and CarId is a property of it.

然后,您需要rowData CarID

var CarID = rowData.CarID

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM