jquery serialize和encodeURIComponent [英] jquery serialize and encodeURIComponent

查看:77
本文介绍了jquery serialize和encodeURIComponent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对表单输入进行URI编码,然后使用一堆隐藏的输入进行序列化并发送到PHP文件..是否有可能以某种方式将encodeURIComponent组合到此行中?:

I need to URI encode a form input, that is then serialized with a bunch of a hidden inputs and sent to a PHP file.. is it possible to somehow combine encodeURIComponent into this line?:

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

更新:

这样做:

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

并输入网址:

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

进入文本框,返回URL不变..不应该将所有破折号和斜线等转换为十六进制代码吗?

into the text box, returns the URL unchanged.. shouldnt it be converting all the dashes and slashes etc to hex codes?

更新

以下是完整代码。

<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>


推荐答案

如果你使用jQuery.ajax,你可以看到传统选项的文档。另外如果使用jQuery 1.4,则在jQuery.param中使用传统。
他们所做的是使用函数encodeURIComponent作为键和值:

if you use jQuery.ajax, you can see the documentation for the option "traditional." Also if you use jQuery 1.4 has the "traditional" in "jQuery.param." what they do is use the function "encodeURIComponent" for the key and value:

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

找到传统设置

jQuery.param

更新

您可以从此示例中看到,序列化在通过邮寄或获得发送的领域工作得很好。
你能把你发送数据的ajax代码?
示例

you can see from this example, "serialize" works well over the fields to be sent by post or get. Can you put the ajax code to which you send data? example

这篇关于jquery serialize和encodeURIComponent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆