在按键事件中,如何将','更改为'〜' [英] On keypress event, how do I change a ',' to a '~'
问题描述
我必须防止Coldfusion的默认列表分隔符','被输入到表单输入数组中。我是新手使用javascript进行验证,并且从未尝试切换某人输入的值。我如何抓住逗号,并将其替换为tilda?
I have to prevent Coldfusion's default list delimiter ',' from being entered into a form input array. I am new to using javascript for validation purposes, and have never tried to switch out the values someone is typing in. How can I snag a comma, and replace it with a tilda?
Javascript我到目前为止尝试过:
Javascript i've tried so far:
$(document).ready(function(event){
var regExComma = /,/;
$("[name='name[]']").live("keypress",function(event){
// i know i could check the numerical value, i feel this requirement can get more added to it and I would like to just change the regEx accordingly.
if(regExComma.test(String.fromCharCode(event.which)){
//it was a ',' switch it to '~'
event.which = 126;
}
});
// added to show that the 'name' input form array is the only input that cares about the ','
var regExDig = /[\d]/
$("[name=min[]],[name=max[]]").live(keypress, function(event){
if(!regExDig .test(String.fromCharCode(event.which)){
event.preventDefault();
$("#cfocFormMessages").trigger("updateMessages", {"url":"components.cfc/CFOC.cfc", "data":{"more":"stuff"}});
}
});
});
cfml / html涉及:
cfml / html involved:
<form action="components/CatagoryService.cfc?method=saveVersion">
<input id="version" name="version" type="text">
<!--- .. more inputs ..--->
<table id="table">
<thead><tr><th>name col</th>
<th>min col</th>
<th>max col</th>
<th>edit</th>
</tr></thead>
<tfoot></tfoot>
<cfoutput query="variables.query">
<tr><td><input name="name[]" type="text" value="#variables.query.name#"></td>
<td><input name="min[]" type="text" value="#variables.query.min#"></td>
<td><input name="max[]" type="text" value="#variables.query.max#"></td>
<td><input name="id[]" type="hidden" value="#variables.query.id#">
<a href="#" class="editLink">edit</a></td>
</tr>
</cfoutput>
<tr><td></td><td></td><td><a href="#" class="addLink">add</a></td></td></tr>
</table>
<input type="Submit"/>
</form>
如果我改变CatagoryService.cfc?method = saveVersion到< cfreturn arguments> ;
在JSON字符串中,Coldfusion的典型响应如下:
if I alter CatagoryService.cfc?method=saveVersion to <cfreturn arguments>
in a JSON string, a typical response from Coldfusion looks like:
{VERSION:"",name:"name1,name2",min:"1,3", max:"2,4",id:"1,2"}
推荐答案
我替换了名称[] .live() event.which = 126;
to event.originalEvent.keyCode = 126;
I replaced the name[] .live() event.which = 126;
to event.originalEvent.keyCode=126;
var regExComma = /,/;
$("[name='name[]']").live("keypress",function(event){
if(regExComma.test(String.fromCharCode(event.which)){
//this line works as expected. and will swap out the value on keypress.
if(event.originalEvent.keyCode){
event.originalEvent.keyCode=126;
}else if(event.originalEvent.charCode){
event.originalEvent.charCode=126;
}
}
});
wolfram我也提升了你的keyUp解决方案。
wolfram I upped your keyUp solution as well.
这篇关于在按键事件中,如何将','更改为'〜'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!