2014-01-13 72 views
2

我想調用多個按鈕相同的ajax功能請幫我下面給出的代碼。此代碼將生成按鈕,當我們點擊按鈕時,它會打印細節...請解決。這段代碼將生成表格中的按鈕。如何使用多個按鈕相同的ajax功能

<head> 
<script> 
$(document).ready(function() 
{ 
$("#save").click(function() 
{ 
var sel = $("#response").val(); 
var fudate=$("#fdate").val(); 
var cmd=$("#cm").val(); 
var id=$("#id").val(); 
$.ajax(
    { 
     type: "POST", 
     url: "autosave.php", 
     data: {response:sel,fdate:fudate,cm:cmd,id:id}, 
     success: function(result) 
     { 
alert('result'); 
      $a= $("#ak").html(result); 
     } 
    }); 
}); 
}); 
</script> 
</head> 
<body> 
<div id="company_details"> 
<div id="ak"></div> 
<table border="1" align="left"> 
<tr> 
<th >Company</th> 
<th>Contact Person</th> 
<th>Contact Person's No.</th> 
<th>HR</th> 
<th>HR's No.</th> 
<th>Current Vacancies</th> 
<th>Response</th> 
<th>Follow Up Date</th> 
<th>Comment</th> 
<th></th> 
<th>Edit</th> 
</tr> 
<?php 
$con=mysql_connect("localhost","root") or die("Error In Connection:-".mysql_error()); 
mysql_select_db("ttcs",$con) or die("Error In DB Selection:-".mysql_error()); 
$sql="select * from company_details"; 
$rs=mysql_query($sql) or die(mysql_error()); 
while($row=mysql_fetch_array($rs)) 
{ 
?> 
<tr> 
<td> 
<input type="text" name="name" value= <?php $e=$row[0];echo $row[1];?>></td>        
<td><input type="text" name="cp" value= <?php echo $row[7]; ?>></td> 
<td><input type="text" name="cpn" value= <?php echo $row[9]; ?>></td> 
<td><input type="text" name="hr" value= <?php echo $row[10]; ?>></td> 
<td><input type="text" name="hrn" value= <?php echo $row[12]; ?>></td> 
<td> 
<?php 
$sql2="select * from vacancies where company_id='$e'"; 
$rs2=mysql_query($sql2); 
if($rs2) 
{ 
while($row2=mysql_fetch_array($rs2)) 
{?> 
<textarea name="cv" value=<?php echo $row2[1];?>></textarea> 
<?php }}?> 
</td> 
<td> 
<select name="response" id="response"> 
<option value="Not interested">Not Interested</option> 
<option value="Not responding">Not Responding</option>         
<option value="follow up">Follow up</option> 
<option value="Interested">Interested</option> 
</select></td> 
<?php 
$sql1="select * from call_details_company where company_id=$row[0]"; 
$rs1=mysql_query($sql1); 
$row1=mysql_fetch_array($rs1); 
?> 
<input type="date" name="fdate" id="fdate" value=<?php echo $row1[3];?>>    
<input type="text" name="cm" id="cm" value=<?php echo $row1[5];?>> 
<button id="save" >save</button> 
</td> 
</tr> 
<?php 
} 
?> 
</body> 
+2

包裝你的Ajax調用的函數裏面,就像任何其他代碼 –

回答

3

元素的ID必須是唯一的,所以在循環中不使用ID,更改按鈕的ID類值,然後使用輸入元素的名稱找到相關因素的TR內

$(document).ready(function() { 
    $(".save").click(function() { 
     var $tr = $(this).closest('tr') 
     var sel = $tr.find("select").val(); 
     var fudate = $tr.find('input[name="fdate"]').val(); 
     var cmd = $tr.find('input[name="cm"]').val(); 
     //need to fix this selector #id too, not able to locate it in the given html structure 
     var id = $tr.find("#id").val(); 
     $.ajax({ 
      type: "POST", 
      url: "autosave.php", 
      data: { 
       response: sel, 
       fdate: fudate, 
       cm: cmd, 
       id: id 
      }, 
      success: function (result) { 
       alert('result'); 
       $a = $("#ak").html(result); 
      } 
     }); 
    }); 
}); 
+0

@ A.沃爾夫謝謝你現在修改 –

+0

它的工作原理,但無法訪問值。它只獲得文本框的第一個值。請幫助查找特定行的特定值 – user3189828

+0

@ user3189828哪些值未找到 –

2

使用這樣

<script> 
function ajaxCallDemo() { 
    var sel = $("#response").val(); 
    var $tr = $(this).closest('tr'); 
    var cmd = $tr.find("input[name='cm']").val(); 
    var fudate = $tr.find("input[name='fdate']").val(); 
    var id = $tr.find("#id").val(); 
    $.ajax({ 
     type: "POST", 
     url: "autosave.php", 
     data: { 
      response: sel, 
      fdate: fudate, 
      cm: cmd, 
      id: id 
     }, 
     success: function(result) { 
      alert('result'); 
      $a = $("#ak").html(result); 
     } 
    }); 
} 
$(document).ready(function() { 
    $("#save").click(function() { 
     ajaxCallDemo(); 
    }); 
    $("#save2").click(function() { 
     ajaxCallDemo(); 
    }); 
}); 
</script> 
+0

+1,但你可以使用參考函數語法:'$(「#拯救」)單擊( ajaxCallDemo);' –

+1

@ A.Wolff仍然是選擇器'var cmd = $(「#cm」)。val()'不起作用 –

+0

@ArunPJohny我現在看到它,thx –

相關問題