替换字符串中的多个字符实例

Som*_*one 3 javascript jquery jquery-ui

<form id="form1" method = "post">
Text1:<input type ="text" id="textname1"/><br>
<input type ="button" name="button2" id="button2" value="UPDATE">
</form>

<script type ="text/javascript">
    $(document).ready(function() {
        $("#button2").click(function(e){
        alert($("#textname1").attr('value').replace('-',''));
            });
        $( "#textname1" ).datepicker();
        $( "#textname1" ).datepicker("option", "dateFormat", 'yy-mm-dd' );

    });
</script>
Run Code Online (Sandbox Code Playgroud)

假设我在2010-07-06字段中输入日期.当我点击button2时,我收到警告为201007-06.如何替换最后一个连字符( - )

Viv*_*ath 7

更改替换函数的正则表达式参数以包含g标志,这意味着"全局".这将取代每个事件而不仅仅是第一个事件.

$("#textname1").attr('value').replace(/-/g,'')
Run Code Online (Sandbox Code Playgroud)