admin管理员组文章数量:1353117
For example, if the field value was "John Wayne" I would want it to be replaced with "John_Wayne"
I'm thinking I can acplish this through jQuery, basic idea is below:
$('#searchform').submit(function() {
//take current field value
//replace characters in field
//replace field with new value
});
Any help is appreciated.
For example, if the field value was "John Wayne" I would want it to be replaced with "John_Wayne"
I'm thinking I can acplish this through jQuery, basic idea is below:
$('#searchform').submit(function() {
//take current field value
//replace characters in field
//replace field with new value
});
Any help is appreciated.
Share Improve this question asked Dec 24, 2011 at 5:35 TimTim 1,4502 gold badges15 silver badges21 bronze badges3 Answers
Reset to default 10You could use the overload of val
that takes a function:
$("input:text").val(function (i, value) {
/* Return the new value here. "value" is the old value of the input: */
return value.replace(/\s+/g, "_");
});
(You'll probably want your selector to be more specific than input:text
)
Example: http://jsfiddle/nTXse/
If you want to look at all of your form elements without specifying them individually, you could do something like:
$('#searchform').submit(function() {
$.each($(':input', this), function() {
$(this).val($(this).val().replace(' ', '_'));
});
});
You might have to pay attention to the type of the element, and that it's visible, enabled, a certain type, etc.
EDIT: I would use Andrew's answer. This was just the first solution that popped into my head. This one might ultimately give you slightly more control over each field in your form, but Andrew's is short and sweet.
Use the onkeyup HTML element attribute and regular expressions:
<input type="text" onkeyup="this.value=this.value.replace(/[Search Expression]/flag,'New String');">
This solution prevents the user from typing a value outside the pattern established by the regex.
For your case, use:
<input type="text" onkeyup="this.value=this.value.replace(/[\s]/g, '_');">
本文标签: javascriptReplace characters in field on form submitStack Overflow
版权声明:本文标题:javascript - Replace characters in field on form submit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743861309a2551786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论