从jquery datatable中选择一个值

时间:2015-08-06 18:08:46

标签: javascript jquery datatables

我无法从此表中获取隐藏输入元素的值。

这是我的表:

<table id="scrapApprovalTable" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th></th>
            <th>Date</th>
            <th>Company</th>
            <th>Notes</th>
            <th>Comments</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <cfoutput>
            <cfloop query="GetRequests">
                <tr class="#specialPricingScrapID#" id="#specialPricingScrapID#">
                    <td class="details-control" value="#specialPricingScrapID#"></td>
                    <td class="date">#DateFormat('#enterDate#', 'mm-dd-yyyy')#</td>
                    <td class="company"><a href="http://www.pblead.com/cfleadsource/MarMgt.cfm?ContactID=#contactid###ContactInfo" target="_blank">#company#</a>
                    </td>
                    <td class="notes">#notes#</td>
                    <td class="comments">
                        <textarea name="processingScrapComments-<cfoutput>#specialPricingScrapID#</cfoutput>" id="processingScrapComments-<cfoutput>#specialPricingScrapID#</cfoutput>" cols="30" rows="5"></textarea>
                    </td>
                    <td class="buttons">
                        <input type="hidden" id="requestID" value="#specialPricingScrapID#">
                        <button class="btn btn-success btn-block btn-small" id="btn-ApproveScrapRequest" name="btn-ApproveScrap" onclick="processRequest(#specialPricingScrapID#, #contactid#, #userid#)">Approve</button>
                        <br>
                        <button class="btn btn-danger btn-block" id="btn-RejectScrapRequest" name="btn-RejectScrap" onclick="processDenial(#specialPricingScrapID#, #contactid#, #userid#)">Deny</button>
                        <br>
                    </td>
                </tr>
            </cfloop>
        </cfoutput>
    </tbody>
</table>

这是我正在使用的javascript:

$(document).ready(function () {
    var approvalTable = $('#scrapApprovalTable').DataTable();
    $('#scrapApprovalTable tbody').on('click', 'td.details-control', function (e) {
        e.stopPropagation();
        var $this = $(this);
        var trid = $this.closest('tr').data('id');
        alert("TR ID " + trid);
        var tdid = $this.find('td[data-id]').data('id');
        alert("TD ID " + tdid);
    });
});

我只是想获得

的价值
  1. 隐藏元素
  2. 或所选TR的ID或
  3. class =&#34; details-control&#39;选择的行。
  4. 所有这些都是相同的值。我想要完成的是获取值,以便我可以做另一个请求。

    感谢大家的帮助。

    此表直接在创建时加载。

2 个答案:

答案 0 :(得分:2)

使用以下代码获取<tr>元素$(this).closest('tr').attr('id'); 属性的值:

private GoogleMap mMap; // Might be null if Google Play services APK is not available.

String Fauna = "51.844188, 8.301594";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    setUpMapIfNeeded();
}

@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

/**
 * Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
 * installed) and the map has not already been instantiated.. This will ensure that we only ever
 * call {@link #setUpMap()} once when {@link #mMap} is not null.
 * <p/>
 * If it isn't installed {@link SupportMapFragment} (and
 * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
 * install/update the Google Play services APK on their device.
 * <p/>
 * A user can return to this FragmentActivity after following the prompt and correctly
 * installing/updating/enabling the Google Play services. Since the FragmentActivity may not
 * have been completely destroyed during this process (it is likely that it would only be
 * stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
 * method in {@link #onResume()} to guarantee that it will be called.
 */
private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

/**
 * This is where we can add markers or lines, add listeners or move the camera. In this case, we
 * just add a marker near Africa.
 * <p/>
 * This should only be called once and when we are sure that {@link #mMap} is not null.0x110000FF
 */
private void setUpMap() {

    mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));

    mMap.addMarker(new MarkerOptions().position(new LatLng(51.844188, 8.301594)).title("Flora und Fauna"));

    // Erschafft einen Neuen Kreis, bei dem Marker Fauna.

    Circle kreis_Fauna_Flora = mMap.addCircle(new CircleOptions()
            .center(new LatLng(51.844188, 8.301594))
            .radius(200)
            .strokeColor(0xffff0000)
            .fillColor(0x44ff0000)
            .strokeWidth(2f));


    // Erlaubt google maps meine Location zu nutzen
    mMap.setMyLocationEnabled(true);

    // Ist dazu da das LocationManager aus getSystemService "importiert" wird wofür LocationManager da ist keine ahnung ...
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);


    // Create a criteria object to retrieve provider (Braucht man um später seine Location zu bestimmen)
    Criteria criteria = new Criteria();

    // Get the name of the best provider (Ein string mit dem namen provider wird erschaffen, per LocationManager.getBestProvider wird der Name des providers wiedergegeben, der am besten zu criteria passt ... braucht man auch um seine  LETZTE Location herauszufinden)
    String provider = locationManager.getBestProvider(criteria, true);

    // Get Current Location (Der finala part mit dem mann seine Location bekommt)
    Location myLocation = locationManager.getLastKnownLocation(provider);

    // set map type (hiermit setzte ich den Kartentyp)
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    // Get latitude of the current location (Bekommtden Breitengrad meiner Position aus myLocation)
    double latitude = myLocation.getLatitude();

    // Get longtitude of the current location (Bekomme den Längengrad meiner Position aus myLocation)
    double longitude = myLocation.getLongitude();

    // Create a LatLng object for the current location (Latlng speichert die Längengrade und Breitengrade als Koordinaten)
    LatLng latLng = new LatLng(latitude, longitude);

    // Show the current location in Google Map (Zeigt die jetzige Location in einer "animation")
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map (Zoomt zu unserer Position, Erschafft danach einen Marker an unserer Position mit der Nachricht "Du bist hier")
    mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Du bist hier!").snippet("Du hast dich selbst localisiert!"));


   // macht nichts ausser Ein Meldungsfenster zu öffnen ...
    new AlertDialog.Builder(this).setMessage("Made by Lars Matthäus").setNeutralButton("ok",null).show();

}

答案 1 :(得分:1)

<强> DEMO

var approvalTable = $('#scrapApprovalTable').DataTable();
$('#scrapApprovalTable tbody').on('click', 'td.details-control', function (e) {
    e.stopPropagation();
    var $this = $(this);
    var trid = $this.closest('tr').attr('id');
    alert("TR ID " + trid);
    var tdid = $this.find('td#' + trid).attr('id');
    alert("TD ID " + tdid);
});