【问题标题】:Allowing ampersand (&) in textarea允许在 textarea 中使用与号 (&)
【发布时间】:2014-11-12 17:16:18
【问题描述】:

我用谷歌搜索了这个但没有运气。

我有一个文本区域,我正在尝试使用 jQuery 通过 ajax 发送。我的问题是当 textarea 包含&符号(&)时它会中断。也就是说,& 之后包含的任何文本都不会被提取,包括与号 (&)。我发送数据如下:

message = "What are the concerns you are looking to address? "+othr+".\n";
var dataString = 'name=' + name + '&email=' + email + '&company=' + company + '&message=' + message + '&othr=' + othr + '&vpb_captcha_code=' + vpb_captcha_code + '&submitted=1';
$.ajax({  

type: "POST",  

url: "contactus-contact-form.php",  

data: dataString ,

我阅读了encodeURIComponent() 并尝试实现相同但它不起作用。有什么指点吗?

编辑:以下获取文本值失败:

message = "What are the concerns or security issues you are looking to address? "+othr+".\n";
var data0 = {name: + name + email: + email + company: + company + message: + message + 
othr: + othr + vpb_captcha_code: + vpb_captcha_code + submitted: "1"};

$.ajax({  

type: "POST",  
url: "contactus-contact-form.php",  
data: data0,
contentType: "application/json; charset=utf-8",
dataType: "json",

【问题讨论】:

  • 您可以在处理之前简单地将所有& 替换为&。否则使用serialize(),或具有命名属性的数据对象,而不是连接字符串(无论如何这很容易出错)。
  • 为什么将数据作为字符串发送?你可以把它作为一个对象发送,jQuery 会处理一切。
  • @TrueBlueAussie 我该如何替换它? textarea 是用户用来发送查询的表单的一部分。
  • @MMM 请看上面的编辑
  • @Sarah:那是无效的Javascript,你想做{name: name, email: email}..

标签: javascript jquery html ajax


【解决方案1】:

& 是用于分隔表单编码数据中的键/值对的字符。如果您想将其用作数据的一部分,则需要对其进行转义。

最简单的方法是让 jQuery 为您构建表单编码字符串。将对象传递给 data 参数:

$.ajax({  
    type: "POST",  
    data: {
        name: name, 
        email: email, 
        company: company, 
        message: message, 
        othr: othr,
        vpb_captcha_code: vpb_captcha_code,
        submitted: 1
    },
    // etc

如果你真的想手动操作,那么encodeURIComponent 函数就可以了:

var dataString = "name=" + encodeURIComponent(name) + "&email=" + encodeURIComponent(email) // etc etc etc

【讨论】:

    【解决方案2】:

    我遇到了这个问题, 出于特定原因,我需要显示 &而不是 & 在文本区域中。 为此,我将“&”替换为“&”

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      您可以使用 encodeURIComponent() 函数解决您的问题。您只需将代码更改如下。我认为您已将 textarea 值设置为变量 othr。您只能使用 encodeURIComponent(othr) 对该变量进行编码。这意味着您可以使用 encodeURIComponent 方法解析 textarea 值。

       message = "What are the concerns you are looking to address? "+othr+".\n";
      var dataString = 'name=' + name + '&email=' + email + '&company=' + company + '&message=' + message + '&othr=' + encodeURIComponent(othr) + '&vpb_captcha_code=' + vpb_captcha_code + '&submitted=1';
      $.ajax({  
      
      type: "POST",  
      
      url: "contactus-contact-form.php",  
      
      data: dataString ,
      });
      


      message = "What are the concerns or security issues you are looking to address? "+othr+".\n";
      var data0 = {name: + name + email: + email + company: + company + message: + message + 
      othr: +encodeURIComponent(othr) + vpb_captcha_code: + vpb_captcha_code + submitted: "1"};
      
      $.ajax({  
      
      type: "POST",  
      url: "contactus-contact-form.php",  
      data: data0,   
      dataType: "json",
      

      【讨论】:

      • 请说明出现的问题
      • contentType: "application/json; charset=utf-8",? ——不要那样做。 data0 不是 JSON。如果你确实将一个对象传递给data(你应该)然后jQuery会为你转义它,所以你不能使用encodeURIComponent作为好吧。
      • 你在建议无效的 JavaScript,你构造的对象都错了......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-05
      • 2016-04-05
      • 2017-03-20
      • 2012-03-09
      • 2010-10-22
      • 2021-07-02
      相关资源
      最近更新 更多