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

Tim*_*Tim 4 javascript jquery

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

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

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

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

}); 
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.

And*_*ker 10

您可以使用val带有函数的重载:

$("input:text").val(function (i, value) {
    /* Return the new value here. "value" is the old value of the input: */
    return value.replace(/\s+/g, "_");
});
Run Code Online (Sandbox Code Playgroud)

(你可能希望你的选择器更具体input:text)

示例: http ://jsfiddle.net/nTXse/