2017-10-24 12 views
1

Hier verwende ich jQuery Tabelle bitte helfen Sie mir. Wie kann ich Id Wert fangen und diesen Wert zur nächsten Funktion bindenmit jQuery wie ID-Wert innerhalb der Tabelle zu fangen

success: function (data) { 
    var $table = $('<table></table>').addClass('table table-responsive table-striped table-bordered'); 
    var $header = $('<thead><tr><th>EmpId</th><th>EmpName</th><th>Email</th><th>Action</th></tr><thead/>'); 
    $table.append($header); 
    $.each(data, function (i, value) { 
    var $row = $('<tr></tr>'); 
    $row.append($('<td/>').html(value.Emp_Id));     
    $row.append($('<td/>').html('<a id="Hello" href="#" value='+value.Emp_Id+'>Edit</a>')) 
    $table.append($row); 
    }); 
    $('#Div1').html($table); 
} 

Here I binden möchten, dass wert-

$('body').on('click', '#Hello', function (e) { 
    var page2 = $(this).valueOf(); 
    console.log(page2);  
}); 
+0

'Wie kann ich Id fangen value'-> die ID-Wert Sie sprechen? –

+0

innerhalb einer Zeile Wen ich auf Bearbeiten klicken Link sein Wert sollte binden –

+0

Versuchen Sie, Wert von '# Hallo' zu bekommen? –

Antwort

1

Sie müssen es tun, wie unten: -

$(document).on('click', 'tr td a', function (e) { //bind click on link 
    e.preventDefault(); //stop page refresh and page url change 
    var page2 = $(this).attr('value'); //get link value 
    console.log(page2); 
}); 

Arbeitsbeispiel: -

$(function(){ 
 
var data=[{Emp_Id:1,Emp_Name:'alive',Email:'[email protected]'},{Emp_Id:2,Emp_Name:'to',Email:'[email protected]'},{Emp_Id:3,Emp_Name:'die',Email:'[email protected]'}]; 
 
var $table = $('<table></table>').addClass('table table-responsive table-striped table-bordered'); 
 
    var $header = $('<thead><tr><th>EmpId</th><th>EmpName</th><th>Email</th><th>Action</th></tr><thead/>'); 
 
    $table.append($header); 
 
    $.each(data, function (i, value) { 
 
     var $row = $('<tr></tr>'); 
 
     $row.append($('<td/>').html(value.Emp_Id)); 
 
     $row.append($('<td/>').html(value.Emp_Name)); 
 
     $row.append($('<td/>').html(value.Email)); 
 
     $row.append($('<td/>').html('<a href="#" value='+value.Emp_Id+'>Edit</a>')); // no need of creating repetitive id="Hello" 
 
    
 
     $table.append($row); 
 
    }); 
 
    $('#Div1').html($table); 
 
    
 
}) 
 
$(document).on('click', 'tr td a', function (e) { //bind click on link 
 
    e.preventDefault(); //stop page refresh and page url change 
 
    var page2 = $(this).attr('value'); //get link value 
 
    console.log(page2); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id="Div1"></div>

1

anstelle von ID = "Hallo" erstellen onclick Funktion in a-Tag selbst.

Verwendung dieser Code in Ihre Erfolgsfunktion

$(function(){ 
 
var data=[{Emp_Id:1},{Emp_Id:2},{Emp_Id:3}]; 
 
var $table = $('<table></table>').addClass('table table-responsive table-striped table-bordered'); 
 
    var $header = $('<thead><tr><th>EmpId</th><th>EmpName</th><th>Email</th><th>Action</th></tr><thead/>'); 
 
    $table.append($header); 
 
    $.each(data, function (i, value) { 
 
     var $row = $('<tr></tr>'); 
 
     $row.append($('<td/>').html(value.Emp_Id));     
 
     $row.append($('<td/>').html('<a href="#" onclick="editEmployee('+value.Emp_Id+')">Edit</a>')) 
 
     $table.append($row); 
 
    }); 
 
    $('#Div1').html($table); 
 
    
 
}) 
 
function editEmployee(id){ 
 
    console.log(id); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<div id="Div1"></div>

+0

ReferenceError: editEmployee ist nicht definiert –

+0

ändern Sie dieses $ row.append ($ ('') .html ('Edit ')) und fügen Sie diese Funktion Funktion editEmployee (id) { console.log (id); } –

+0

Obwohl das gleiche Ergebnis @kalaiselva –

Verwandte Themen