2010-11-25 154 views
5

我需要URI編碼形式的輸入,也就是,然後用一束的隱藏的輸入串行化並將其發送到一個PHP文件..是有可能以某種encodeURIComponent方法結合成此行?:jquery的序列化和encodeURIComponent方法

var landingCreate = $(this).serialize(); 

UPDATE:

這樣做,例如:

var landingCreate = $(this).serialize()+"&enc="+encodeURIComponent($('input[name=\'longform\']').val()); 

並輸入URL:

http://www.smashingmagazine.com/2008/10/13/pricing-tables-showcase-examples-and-best-practices/ 

進入文本框,返回的URL不變..不應該它將所有破折號和斜槓等轉換爲十六進制代碼?

UPDATE

下面是完整的代碼。

<form id="createTokenLanding"> 
    <input type="text" name="longform" /> 
    <input type="hidden" name="domain" value="<?php echo rawurlencode($_SERVER['HTTP_HOST']); ?>" /> 
    <input type="hidden" name="useragent" value="<?php echo rawurlencode($_SERVER['HTTP_USER_AGENT']); ?>" /> 
    <input type="hidden" name="ip" value="<?php echo rawurlencode($_SERVER['REMOTE_ADDR']); ?>" /> 
    <input type="hidden" name="cookieuser" value="<?php echo rawurlencode($_COOKIE['littlr_user']); ?>" /> 
    <input type="submit" name="submit" value="Shorten" /> 
</form> 

<div id="result"> 
123 
</div> 

<script type="text/javascript"> 
    $(document).ready(function(){ 
     $.ajaxSetup ({ cache: false }); 
     $('#createTokenLanding').submit(function() { 
      var landingCreate = $('#createTokenLanding').serialize(); 
      $.ajax({ 
       url: 'action-create.php', 
       data: landingCreate, 
       success: function(responseText){ 
         $('#result').html(responseText); 
       } 
      }); 
      return false; 
     }); 
    }); 
</script> 
+0

您想要傳遞URI編碼的輸入是由用戶輸入還是隱藏的輸入? – 2010-11-25 14:22:38

+0

它由用戶輸入。 – Gary 2010-11-25 14:23:56

+0

「longform」的形式是「this」?你正在使用事件提交來發送Ajax數據? – 2010-11-25 15:06:59

回答

4

如果您使用jQuery.ajax,您可以看到選項「傳統」的文檔。另外如果你使用jQuery 1.4在「jQuery.param」中有「傳統」。 他們做的是使用功能 「encodeURIComponent方法」 爲重點和值:

param = encodeURIComponent (key) + "=" + encodeURIComponent (value); 

find traditional setting
jQuery.param

UPDATE

你可以從這個例子中看到, 「序列化」 工程遠遠超過通過郵寄或獲得的領域。 你可以把你發送數據的ajax代碼? example