我有这个样本
代码 HTML:
<form method="post" class="add-patient">
<div class="col-md-12">
<fieldset>
<label for="lastname">Phone<span class="star">*</span></label>
<input class="required-input _phone" id="primary_phone" type="text" name="phone" maxlength="10" value="<?php echo $prInfo->clinicphone; ?>" placeholder="1234567890">
</fieldset>
</div>
<div class="col-md-12">
<div class="pull-right" id="save-bottom-add">
<button type="Submit" id="btn-sub" class="btn btn-primary btn-save" onclick="DoSubmit()">Submit</button>
</div>
</div>
</form>
代码 JS:
function DoSubmit(){
var firstFormat = $("#primary_phone").val(); //ex format:(123) 123-1234
var lastFormat = firstFormat.replace(/\s/g, ''); //the new format should be: 123-123-1234
console.log(lastFormat);
return true;
}
我想做的是在提交之前转换输入的格式文本,并将其以新格式传递给 POST
我们选择的方法正确吗? 转换这种格式的解决方案是什么?
你能帮我找到解决这个问题的方法吗? 提前致谢!
最佳答案
这样的事情应该让你走上正轨
function DoSubmit(){
var firstFormat = $("#primary_phone").val(); //ex format:(123) 123-1234
var lastFormat = firstFormat.replace(/\D/g,"").replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3'); //new format: 123-123-1234
console.log(lastFormat);
// also do any substitution of value(s) here
// Ex:- $("#primary_phone").val(lastFormat);
return true;
}
$(".add-patient").submit(function() {
DoSubmit();
});
.submit()
是第一个变体中 .on( "submit", handler )
的快捷方式,而 .trigger( "submit")
在第三个。参见 docs
更新:感谢@titus 指出非数字字符。 查看更新后的代码和 this fiddle用于演示。
关于javascript - 如何更改邮寄时传输的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38094237/