2016-04-28 5 views
0

EDITZugriff auf den Inhalt eines bestimmten td aber nicht in der Lage sein Inhalt

<table class="table table-hover" id="patient_name_table"> 
       <tr id="after_tr_2"> 
        <th>Patient</th> 
        <th>Phone</th> 
        <th>D.O.B</th> 
        <th>Address</th> 
        <th>Edit</th> 
       </tr> 
       <tbody> 
        <?php foreach($name_array as $tabName) { ?> 
        <tr id="<?php echo $tabName['id']; ?>"> 
        <td class="nameid"><?php echo '<a href="patients.php?patient='.$tabName['id'].'">'.$tabName['patient_name'].'</a>';?></a> 
        </td> 
        <td class="phoneid"><?php echo $tabName['phone']?></td> 
        <td class="dobid"><?php echo $tabName['dob']?></td> 
        <td class="addressid"><?php echo $tabName['patient_address']?></td> 
        <td><button type="button" class="btn btn-info" id="editInfo">Edit</button> 
        </tr> 
        <?php } ?> 
       </tbody> 
       </table> 

Und hier ist das Update-Formular zu ändern:

<div class="box" id="dialog" title="Update Patient Informations" style="display:none"> 
        <form class="form-horizontal" id="add_app_form"> 
        <table class="table table-striped table-hover" style="width:650px;"> 
         <tr><th style="text-align:left;width:150px">Patient Name:</th> <td><input type="text" class="form-control" id="name_upd"></td></tr> 
         <tr><th style="text-align:left;width:150px">Phone:</th> <td><input type="text" class="form-control" id="phone_upd"></td></tr> 
         <tr><th style="text-align:left;width:150px">D.O.B:</th><td><input type="text" class="form-control" id="dob_upd"></td></tr> 
         <tr><th style="text-align:left;width:150px">Address:</th> <td><input type="text" class="form-control" id="address_upd"></td></tr> 
        <tr><td colspan="3"><button type="button" id="upd_info" class="btn btn-info pull-right">Update</button></td></tr> 
        <table> 
       </form> 
       </div> 

ich diese Zeile, wo ich die ID erhalten eine bestimmte Zeile in meiner Tabelle:

var id = $(this).closest('tr').attr('id'); 

Jetzt muss ich eine specifi bekommen c Informationen von diesen tr, und aus dem Namen td, die ein class=nameid hat, mit dieser Linie:

var x = $(this).parent('td').parent('tr').children('td.nameid').text(); 

Das Problem ist jetzt, dass ich nicht x Zugriff auf den Wert in eine neuen innen ein ändern Erfolgsfunktion von AJAX-Aufruf. Hier

ist der Ajax-Aufruf:

$("#upd_info").on('click', function() 
     { 
     var upd_name = $("#name_upd").val(); 
     var upd_dob = $("#dob_upd").val(); 
     var upd_phone = $("#phone_upd").val(); 
     var upd_address = $("#address_upd").val(); 
     $.ajax({ 
      url: 'upd_patient.php', 
      data: {upd_id: id, n: upd_name, d: upd_dob, p:upd_phone, a:upd_address}, 
      type: 'POST', 
      dataType: 'JSON', 

      success:function(res3) 
      { 
      $("#dialog").dialog("close"); 
      //here I need to access td of class nameid and change the value 
      }, 
      error:function(res3) 
      { 
      console.log("Error Updating info"); 
      } 
     }); 

Gibt es einen Weg für sie?

Antwort

0

Text wird Ihnen den Text von innen zurückgeben, nicht der Accessor zum Text. Versuchen Sie x als $(this).parent('td').parent('tr').children('td.nameid') verlassen, und immer den Text mit x.text(), und dann mit x.text('new text') Einstellung

0

success Rückruf hat this besitzen. Sie sollten die Referenz .nameid vor dem Ajax-Aufruf erhalten und den Text in success wie folgt ändern.

$("#upd_info").on('click', function(){ 
    var upd_name = $("#name_upd").val(); 
    var upd_dob = $("#dob_upd").val(); 
    var upd_phone = $("#phone_upd").val(); 
    var upd_address = $("#address_upd").val(); 

    //added following line 
    var x = $(this).closest('tr').find('td.nameid'); 

    $.ajax({ 
     url: 'upd_patient.php', 
     data: { upd_id: id, n: upd_name, d: upd_dob, p: upd_phone, a: upd_address }, 
     type: 'POST', 
     dataType: 'JSON', 

     success: function(res3) { 
      $("#dialog").dialog("close"); 

      //added following line 
      x.text('changed text'); 
     }, 
     error: function(res3) { 
      console.log("Error Updating info"); 
     } 
    }); 
}); 
+0

nein, es hat nicht funktioniert, nichts geändert – androidnation

+0

Sind Ihre Elemente dynamisch generiert? @androidnation – Azim

+0

Ja, die Tabelle wird mit PHP erzeugt, wo sie Werte von db erhalten und jede Zeile eine spezifische ID hat – androidnation

Verwandte Themen