javascript - 在按键事件中,如何将 ',' 更改为 '~'

标签 javascript jquery coldfusion-9

我必须防止将 Coldfusion 的默认列表分隔符“,”输入到表单输入数组中。我刚开始使用 javascript 进行验证,并且从未尝试过切换出某人输入的值。我怎样才能捕获一个逗号,并将其替换为波浪线?

到目前为止我尝试过的 Javascript:

    $(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 涉及:

<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 的典型响应如下所示:

{VERSION:"",name:"name1,name2",min:"1,3", max:"2,4",id:"1,2"}

最佳答案

我把your HTML on jsfiddle (你可以在那里测试它)并添加这个 JavaScript,使用匹配所有和元素的选择器:

$(document).ready(function(event){
    $(document).delegate("input, textarea", "keyup", function(event){
        if(event.which === 188) {
            var cleanedValue = $(this).val().replace(",","~");
            $(this).val(cleanedValue);
        }
    });
});

如果输入逗号(代码 188),则值字符串中的所有逗号都将替换为波浪号。

请记住,JavaScript 验证不是您想要依赖的。逗号可以很容易地发送到服务器或永远不会被替换,例如在禁用 JavaScript 的用户代理中。

关于javascript - 在按键事件中,如何将 ',' 更改为 '~',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5266522/

相关文章:

javascript - 单击动态生成的 div - 没有 ID 和类,使用 jquery 嵌套在静态 div 中

javascript - MVC : Plugging a C++ Model into a Web-based View

mysql - Coldfusion中如何对mysql数据库进行加密和使用

coldfusion - 在 cfquery 中嵌入 cfswitch

Javascript 旋转数据并从对象生成动态表

javascript - 在不同的内容上平滑地更改 div 高度

javascript - 未捕获的类型错误 : Cannot read property '0' of null

javascript - 如何在 HTML 标签上添加类

javascript - 为什么用 PHP 动态生成的 HTML 不响应 JavaScript?

coldfusion - 在 Coldfusion/Apache POI 中强制完整计算整个工作簿