2015-06-03 63 views
0

allmob.js:爲什麼IAM越來越黑屏在此代碼

$(document).ready(function() { 
    $(':checkbox').change(function() { 
     var result1 = []; 

     $(':checkbox:checked').each(function() { 
      result1.push($(this).val()); 
     }); 

     var url = "mob1.php" 
     if (result1.length > 0) { 
      //var fin = result1 ; 
      $.post(url, { contval: result1 }, function(data) { 
       $('#result').html(data); 
      }); 
     } 
     else { 
      $('#result').html("nothing is checked"); 
     } 
    }); 
}); 

如果強似result1我通過1像$.post(url, { contval: 1 }, function(data)我得到的輸出,如果我通過result1空白屏幕出現。

mob.php:

<?php 
    $contval = $_POST['contval'] ; 

    if ($contval == "1") 
    { 
     echo "1 is checked" ; 
    } 
    if($contval == "2") 

    { 
     echo "2 is selected" ; 
    } 
?> 

allmob.php

<?php 

    session_start(); 
    ?> 
    <html> 
    <head> 

     <link rel="stylesheet" type="text/css" href="css/bootstrap.css"> 
    <link rel="stylesheet" type="text/css" href="css/allmob.css"> 
    <script type = "text/javascript="js/jquery1.11.3.js">      </script> 
      <script type = "text/javascript" src ="js/allmob.js"></script> 
     </head> 
    <body> 
    <div class = "well">Add2Kart</div> 
    <div class = "container"> 
    <div class = "row"> 
    <div class = "col-lg-4"> 
    <h5>Price :</h5> 
    <input type = "checkbox" name = "price" value = "1">&#8377;1,000-&#8377;5,000</input><br> 
    <input type = "checkbox" name = "price" value = "2">&#8377;5,000-&#8377;10,000</input><br> 
     <input type = "checkbox" name = "price" value = "3">&#8377;10,000-&#8377;15,000</input><br> 
     <input type = "checkbox" name = "price" value = "BETWEEN 15000 AND 25000">&#8377;15,000-&#8377;25,000</input><br> 
     </div> 


      <div class = "col-lg-8"> 
      <div id = "result" > 

      </div> 
     </div> 
     </div> 
    </body> 
     </html> 

選擇複選框被allmob.js處理和選定複選框的值被髮送到mob.php所以我想如果值1複選框得到選中,然後在mob.php條件1打印如果機器人1和2複選框得到選擇兩個條件應打印

+0

在你的第一個例子中的值不能籤,你傳遞值的數組。在你的第二個你傳遞一個int。因此,在PHP中進行相等性測試本質上是有缺陷的。 –

+0

'$(':checkbox')'我懷疑這個..你能添加你的html標記嗎? – roullie

回答

1

嘗試th是...

$_POST['contval']是一個數組,所以它直接使用「==

<?php 
    $contval = $_POST['contval'] ; 

    if (!empty($contval) && in_array("1", $contval)) 
    { 

     echo "1 is checked" ; 
    } 
?> 
相關問題