2013-09-23 25 views
0

我有一個html表格,第一列是複選框列表(與表格行一樣多)都有id="checkbox1", "checkbox2", "checkbox3" ....在點擊一個按鈕(id="modifyLink")我需要找到第一個選中的複選框(如果在第一個之後有任何疑問,請不要打擾),然後打開我的模式(​​)並加載一個php頁面併發布選中的複選框的值。Javascript/Ajax/jquery修改select記錄

這是我所推出了(顯然不工作):

$function(){ 
    $("#modifyLink").click(function(){ 
     var checkbox = 'checkbox1'; 
     for(var i=2; getElementById(checkbox).checked == 1; i++){ 
      checkbox = 'checkbox'.i; 
     } 
     if(getElementById(checkbox)==true){ 
      var id = getElementById(checkbox).val(); 
      $(#modModal).modal('show'); 
      $(#modModal).innerhtml = 'load my modal with index.php/trainings/mod passing the id variable possibly in POST'; 
     }); 
}; 

如果我要對此完全錯誤可以有人可以建議完全不同的東西?

編輯:

好,我在研究這個了一點,走了這一點:

$(function() { 
    $("#trainingRow").click(function() { 
    var id = $("#trainingRow").attr('data-value'); 
    var dataString = 'id='+ id; 
    $.ajax({ 
     type: "POST", 
     url: "/index.php/trainings/showDetail", 
     data: dataString, 
     dataType: 'json', 
     success: function(data) { 
      $("div#detailModal").html(data.html); 
      $("div#detailModal").modal('show'); 
     } 
    }); 
    return false; 
    }); 
}); 

但爲什麼沒有在div#detailModal HTML更新?我已經檢查了php函數通過var_dump返回的內容,以及它正好是我感興趣的HTML代碼,但是我不知道爲什麼它不起作用(返回的變量被稱爲html,我在php之前使用了json_encode。把它返還)。 擡起頭就好了,我想我有一點閱讀:)走多遠

回答

0

解決它取代var checkedBox線,我的AJAX實際上是正確的(第二次輪)。問題是,即使你使用json_encode,你也不能從PHP返回變量。你需要對它們進行json_encode並將它們回顯!

感謝大家的意見!

0

你的代碼是非常醜陋...但你wan't像這樣(未經)

$(function)({ 
    $("#modifyLink").click(function(){ 
     var checkbox = 'checkbox', 
      current = null, 
      i = 0; 
     while (document.getElementById(checkbox + i).length == 1){ 
      current = $(document.getElementById(checkbox + i)); 
      if (current.is(':checked')) 
      { 
       var id = current.val(); 
       $.post('url', { id: id }, function(content) { 
       $('#modModal').modal('show'); 
       $('#modModal').html(content); 
       }); 
      } 
      i += 1; 
     } 
}); 
0

如果您可以爲所有複選框指定一個類,請說「myCheckboxClass」,這樣做更容易。那麼你需要的是:

$function(){ 
    $("#modifyLink").click(function(){ 
     var checkedBox = $(".myCheckboxClass:checked").first(); 
     var val = checkedBox.val(); 
     $(#modModal).modal('show'); 
     $(#modModal).innerhtml = 'load my modal with index.php/trainings/mod passing the id variable possibly in POST'; 
     }); 
}; 

或者,如果你知道表的id, 「myTableId」,你可以做一個類似的選擇,只是

var checkedBox = $("#myTableId input:checkbox:checked).first(); 
+0

我知道表ID,所以我可以工作,更清潔的解決方案。我今晚會在下班回家的路上嘗試(我在GMT + 1)。有沒有辦法讓innerhtml或類似的命令從網址讀取?或者是使我的模式和文本輸入或其他什麼,然後自動完成他們一旦我有ID的最佳方式? – James

+0

我使用的唯一模式是jqueryUI對話框,它具有模式模式。 http://jqueryui.com/dialog/#modal-form你可以使它成爲一個模式對話框,並且當你想要的url完成加載時,很容易替換它現有的html。 (基本上,當onLoad觸發時,改變對話框div的內容,或者關閉對話框並打開另一個對話框。) –