2014-04-07 167 views
0

我只是新的使用Ajax和我正在從這個鏈接的教程http://ricochen.wordpress.com/2011/08/02/passing-array-of-checkbox-values-to-php-through-jquery-example/
但我無法使它的工作,你可以請幫助我嗎?Ajax和複選框陣列

的html代碼:

<html> 
<head> 
<script src="jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script> 
<script type="text/javascript"> 
       function doit() { 
         var p=[]; 
         $('input.cb').each(function() { 
           if($(this).attr('checked')) { 
             p.push($(this).attr('rel')); 
           } 
         }); 
         $.ajax({ 
           url:'/test2.php', 
           type:'POST', 
           data: {list:p}, 
           success: function(res) { 
             alert(res); 
           } 
         }); 
       } 
     </script> 
</head> 
<body>  
<input type="checkbox" class="cb" rel="1"></input>This is 1<br /> 
     <input type="checkbox" class="cb" rel="abc"></input>This is abc<br /> 
     <input type="checkbox" class="cb" rel="999"></input>This is 999<br /> 
     <a href="javascript:void(0)" onclick="doit()">Click</a>  
</body> 
</html> 

test2.php:

<?php 

     print_r(@$_POST['list']); 

?> 
+0

而正是不工作? – adeneo

+0

嘗試只是功能DOIT(後提醒的東西){並檢查它是否正在提示.. – Jenz

+0

@jenz我試圖用這個和更換的onClick功能和它的工作職能QWE() \t \t \t \t { \t \t \t \t alert('test'); \t \t \t \t} – zxc

回答

1

試試這個:
1:你使用<input></input>這是錯誤的! <input type='checkbox' name='' />
2:你每次都checkbox您可以將每個$('input.cb:checked')和刪除if
3:你要發佈的AJAX所以使用$.post

<html> 
<head> 
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript" charset="utf-8"></script> 
<script type="text/javascript"> 
       function doit() { 
         var p=[]; 
         $('input.cb:checked').each(function(index,value) {         
             p.push($(value).attr('rel')); 
         }); 
         $.post('/test2.php',{list:p}).done(function(res) {alert(res);}); 
       } 
     </script> 
</head> 
<body>  
     <input type="checkbox" class="cb" rel="1"/>This is 1<br /> 
     <input type="checkbox" class="cb" rel="abc"/>This is abc<br /> 
     <input type="checkbox" class="cb" rel="999"/>This is 999<br /> 
     <a href="javascript:void(0)" onclick="doit()">Click</a>  
</body> 
</html> 
+0

我試過使用它並移除這部分$ .post('/ test2.php',{list:p})。完成(功能(水庫)沒有警報顯示 – zxc

+1

,因爲這是不是'完成'可能沒有找到你的PHP文件,我測試它並給予警報'陣列 ( [0] => 1 [1] => abc )'您的php文件必須在您的html文件的網頁服務器如果不工作更改文件地址並在test2.php前刪除'/' –

+0

確定您保存了我的一天!!!!!如果我可以檢查這是一個aswer三次我會tnx! – zxc

0

使用的onclick = 「」 attribut是一種過時+你should'nt使用鏈接的動作,你應該使用按鈕。鏈接是用於頁面和按鈕的動作。 http://jsfiddle.net/tLq8L/1/ < - 在這裏檢查它的工作版本

<script> 
function doit() { 

        var p=[]; 
        $('input.cb').each(function() { 
          if($(this).attr('checked')) { 
            p.push($(this).attr('rel')); 
          } 
        }); 
        $.ajax({ 
          url:'/test2.php', 
          type:'POST', 
          data: {list:p}, 
          success: function(res) { 
            alert(res); 
          } 
        }); 
      } 
</script> 
    <input type="checkbox" class="cb" rel="1"></input>This is 1<br /> 
    <input type="checkbox" class="cb" rel="abc"></input>This is abc<br /> 
    <input type="checkbox" class="cb" rel="999"></input>This is 999<br /> 
    <a href="javascript:void(0)" onclick="doit()">Click</a>  
+0

小提琴不起作用 – SagarPPanchal

+0

它的工作檢查使用像httpfox或chrome developper這樣的工具來查看http請求,當然,它的結尾是錯誤的,因爲我沒有test2.php – Su4p

+0

http://jsfiddle.net/tLq8L/3/ < - 相同但處理錯誤狀態 – Su4p

1

與此

$('input.cb').each(function() { 
     if($(this).is(':checked')) { // change this line in your code 
      p.push($(this).attr('rel')); 
     } 
}); 

DEMO

+0

好吧,我愛你!最後它的作品!如果可以,我可以幫助我進一步實現目標嗎? – zxc

+0

@zxc是的告訴我':-)' –

+0

tnx!嗯,我需要將數組複選框傳遞給test2.php,我在test2.php中做的是$ list = $ _ POST ['list'];即時通訊ajax後部分有問題 – zxc

0

if($(this).prop('checked'))代替if($(this).attr('checked'))嘗試將解決您的問題