检索输入表单中的特殊字符

时间:2016-09-27 21:36:03

标签: javascript

我有一个包含不同输入字段的HTML表单(大多数是文本值),但只要填充了一个额外的字符(例如%),我就无法检索它。

这是HTML表单:

<form id="myform" class="form-horizontal" action="javascript:notif()" >
  <fieldset>
    <div class="control-group">
      <label class="control-label" for="focusedInput">nom</label>
      <div class="controls">
        <input name="nom" class="input-xlarge focused" id="focusedInput" type="text" value="">
      </div>
    </div>

    <div class="control-group">
      <label class="control-label" for="date01">Date</label>
      <div class="controls">
        <input type="text" name="date" class="input-xlarge datepicker" id="date" value="">
      </div>
    </div>

    <div class="control-group">
      <label class="control-label" for="focusedInput">Titre</label>
      <div class="controls">
        <input name="page" class="input-xlarge focused" id="focusedInput" type="text" value="">
       </div>
    </div>  
...

和用于检索字段的Javascript:

var s_data = $('#myform').serializeArray();
            $.get("webAdd?nom="+s_data[0].value+"&date="+s_data[1].value+"&titre="+s_data[2].value+"&message="+s_data[3].value+"&page="+s_data[4].value+"&commentaires="+s_data[5].value,function(response) {});

我的问题很简单,但我无法解决它:只要任何s_data [x]字段包含诸如&#34; 25%折扣&#34;检索到的文本字段为空。

我知道%字符用于其他目的但是如何检索具有任何特殊字符的字段?

2 个答案:

答案 0 :(得分:1)

由于您要请求网址,因此您需要对用户输入进行编码。否则,将错误地解析URL。要执行此操作,请将from itertools import count c = count(start) lst = [[x, next(c)] for x in seq] 中的每个s_data[0].value包裹在此encodeURIComponent中。 这将对特殊字符进行编码,因此它们可以成为URL的一部分而不会破坏它。

encodeURIComponent(s_data[0].value)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

答案 1 :(得分:0)

你必须对传递给jQuery的查询字符串进行编码,但是看到你的表单输入有你在查询字符串中使用的名字,如果你让jQuery为你工作,这应该是直截了当的

$.get("webAdd", $('#myform').serialize()).then(function(response) {

});