2012-08-12 16 views
0

我有一個jQuery過濾顏色查詢中顯示的一些項目。PHP的jQuery傳遞多個單詞的值

問題出現諸如「藍色海軍」之類的顏色。顯然,jQuery不會傳遞這兩個單詞或其他東西。當我回顯_GET值的結果時,如果傳遞了2個單詞的顏色,我從來沒有收到任何內容,而我確實獲得了單字顏色的值。

任何想法爲什麼發生這種情況?

這裏是我的代碼:

jQuery(document).ready(function($) { 
    $("input:checkbox").change(function() { 
     if($(this).is(':checked')) 
      { 
     $(".loadingItems").fadeIn(300); //fade in on change 
     var color = $(this).val(); 
      $(".indexMain").load('indexMain.php?color='+color,function(){ 
      $(".indexMain").fadeIn(slow); 

      }) 
      $(".loadingItems").fadeOut(300); //remove when load is complete 
     } 
     else 
      { 
      $(".loadingItems").fadeIn(300); //fade in on change 
$(".indexMain").load('indexMain.php'); 
         $(".loadingItems").fadeOut(300); //remove when load is complete 
      } 
     }); 
    }); 

$color = $_GET['color']; 

$items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1"); 
     $items -> bindValue(":colorbase1", $color); 
+0

解釋嘗試'變種顏色= $(本).VAL()。的toString()。REPLACE(」 ',' - ');' – PitaJ 2012-08-12 16:43:49

+0

@PitaJ,它不應該是'+'而不是'-'嗎?另外,'.val()'返回一個字符串。 – Adi 2012-08-12 16:45:44

回答

3

由於它是一個GET請求,該值被附加到查詢字符串,以及查詢字符串不能包含常規空格。

您需要在查詢字符串使用前URL編碼值:

$(function() { 
    $("input[type='checkbox']").on('change', function() { 
     if (this.checked) { 
      $(".loadingItems").fadeIn(300); 
      var color = encodeURI(this.value); 
      $(".indexMain").load('indexMain.php?color=' + color, function() { 
       $(".indexMain").fadeIn(slow); 
       $(".loadingItems").fadeOut(300); 
      }); 
     } else { 
      $(".loadingItems").fadeIn(300); 
      $(".indexMain").load('indexMain.php', function() { 
       $(".loadingItems").fadeOut(300); 
      }); 
     } 
    }); 
}); 

$color = $_GET['color']; 

$items = $con - > prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1"); 
$items - > bindValue(":colorbase1", $color);​ 

$_GET超全局應自動網址在你的PHP腳本解碼回。

0
var color = $(this).val().replace(/ /g, '+'); 

這會在你的情況下工作,但更好的方法是使用是encodeURI由adenneo