简体   繁体   English

读取HTML格式的逗号分隔值

[英]Read comma-separated values in HTML form

I have a simple HTML input element: 我有一个简单的HTML输入元素:

<input type="text" class="form-control" name="value">

This field could have comma-separated values eg ABC, DEF, GHI . 该字段可以具有逗号分隔的值,例如ABC, DEF, GHI The field value when submitted must be exactly the same as when entered. 提交时的字段值必须与输入时完全相同。 However, when I am printing the field value to the console, I am getting 但是,当我将字段值打印到控制台时,

ABC%2C+DEF%2C+GHI . ABC%2C+DEF%2C+GHI

I want ABC, DEF, GHI 我要ABC, DEF, GHI

I tried things like decodeURIComponent and accept-charset="ISO-8859-1" for the form, but they don't work. 我为表单尝试了诸如decodeURIComponentaccept-charset="ISO-8859-1"之类的方法,但是它们不起作用。 How can I prevent the encoding of the commas and spaces? 如何防止逗号和空格的编码? Thanks in advance! 提前致谢!

Before submiting, encode the value and it should work, according to my test 根据我的测试,在提交之前,对值进行编码,它应该可以工作

<form id="myForm" action="form.php" method="GET">
    <input id="encodeMe" name="string" value="this will be encoded correctly" />
    <input type="submit" value="OK" />
</form>

$('#myForm').submit(function() {
    var enc = escape($("#encodeMe").val());
    $("#encodeMe").val(enc);
});

Ok, I got it. 好,我知道了。 In JavaScript, the input field has to be handled thus: 在JavaScript中,必须这样处理输入字段:

decodeURIComponent(str.replace(/\+/g,' '))

where str = ABC%2C+DEF%2C+GHI . 其中str = ABC%2C+DEF%2C+GHI Only decodeURIComponent is not enough. decodeURIComponent是不够的。 Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM