Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 替换表单提交上字段中的字符_Javascript_Jquery - Fatal编程技术网

Javascript 替换表单提交上字段中的字符

Javascript 替换表单提交上字段中的字符,javascript,jquery,Javascript,Jquery,例如,如果字段值为“John Wayne”,我希望将其替换为“John_Wayne” 我想我可以通过jQuery实现这一点,基本思想如下: $('#searchform').submit(function() { //take current field value //replace characters in field //replace field with new value }); 感谢您的帮助 您可以使用的重载具有以下功能: $("input:text").val(func

例如,如果字段值为“John Wayne”,我希望将其替换为“John_Wayne”

我想我可以通过jQuery实现这一点,基本思想如下:

$('#searchform').submit(function() {

//take current field value
//replace characters in field
//replace field with new value

}); 

感谢您的帮助

您可以使用的重载具有以下功能:

$("input:text").val(function (i, value) {
    /* Return the new value here. "value" is the old value of the input: */
    return value.replace(/\s+/g, "_");
});
(您可能希望选择器比
input:text
更具体)


示例:

如果希望查看所有表单元素而不单独指定它们,可以执行以下操作:

$('#searchform').submit(function() {
    $.each($(':input', this), function() {
        $(this).val($(this).val().replace(' ', '_'));
    });
});
您可能需要注意元素的类型,以及它是可见的、已启用的、特定类型的,等等



编辑:我会使用安德鲁的答案。这只是我想到的第一个解决办法。这一个可能最终会让你对表单中的每个字段有更多的控制,但Andrew的字段又短又甜。

+1:我不知道存在过载。这对jQuery 1.4+很好。您不应该在replace上使用
g
global吗<代码>。替换(/\s+/g,“u”,“g”)@Indranil:你完全正确,我忘记了
标志
参数是非标准的。谢谢你的更正!