多选表单值字符串

时间:2017-02-24 17:59:39

标签: php jquery html

我需要一个多选形式,如下所示:

<!doctype html>
<html lang="en">
<head>
</head>
<body>
 
<form action="" method="get">
<p>
  <select class="multiple normal-selectbox" id="RW" name="RW" multiple="multiple">
<option  value="N" ><span class="input-group"><strong>Region Nord</strong></span></option>
   
    <option  value="HAM" ><span class="input-group">Hamburg</span></option>
    <option  value="HAJ" ><span class="input-group">Hannover</span></option>
    <option  value="BRE" ><span class="input-group">Bremen</span></option>
   
  </select>
<div class="form-group submit f0 " data-fid="f0" style="position: relative;">
  <div class="progress" style="display: none; z-index: -1; position: absolute;">
    <div class="progress-bar progress-bar-striped active" role="progressbar" style="width:100%">
    </div>
  </div>

  <button type="submit" class="btn btn-primary btn-lg" style="z-index: 1;">
  		GO
  </button>
  
</form>
</body>
</html>

提交之后,它将URL中的值作为字符串获取它:

... RW = N&安培; RW = HAM&安培; RW = HAJ&安培; RW = BRE

但我需要它:

Rw = HAMxHAJxBRE ......

我找到了这个

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        var x = $("form").serializeArray();
        $.each(x, function(i, field){
            $("#results").append(field.name + "x" + field.value + " ");
        });
    });
});
</script>
</head>
<body>

<form action="" method="get">
  First name: <input type="text" name="FirstName" value="Mickey"><br>
  Last name: <input type="text" name="LastName" value="Mouse"><br>
</form>

<button>Serialize form values</button>

<div id="results"></div>

</body>
</html>

但它没有获取值到URL

你有什么想法解决我的问题吗?

此致

你好朋友,谢谢你的帮助,我做了一些代码,结果是我想把它串到URL我不知道为什么

hier是代码

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        var x = $("form").serializeArray();
        $.each(x, function(i, field){
            $("#results").append("" + field.value+"x");
        });
    });
});
</script>
</head>
<body>

<form action="" method="get">
  <label>
  <select name="RW" multiple class="multiple normal-selectbox" id="RW">
    <!--<option value="-" selected="selected" >alle Abflugh&auml;fen</option>-->
    <optgroup label="Norden">
    <option  value="N" ><span class="input-group">Region Nord</span></option>
    <option  value="HAM" ><span class="input-group">Hamburg</span></option>
    <option  value="HAJ" selected ><span class="input-group">Hannover</span></option>
    <option  value="BRE" ><span class="input-group">Bremen</span></option>
   
  </select>
  </label>
</form>
<button>Serialize form values</button>

<div id="results"></div>

</body>
</html>

字符串也应该是RW =“results”

1 个答案:

答案 0 :(得分:0)

假设您在服务器端使用PHP - 正如本文中PHP标记所暗示的那样,PHP应该自动将其显示为数组。对于普通字段,$_REQUEST['RW']将是用于多重选择的单个值(例如,“HAM”),它将是数组(例如,["HAM","HAJ"])。让PHP为您完成工作,不要尝试自己解码URL。