如何在提交表单之前删除自动数字格式
How can I remove AutoNumeric formatting before submitting form?
我使用的是jQuery插件AutoNumeric,但当我提交表单时,我无法删除POST
之前字段的格式。
我尝试使用$('input').autonumeric('destroy')
(和其他方法(,但它将格式保留在文本字段上。
如何将未格式化的数据POST
发送到服务器?如何删除格式?在初始配置中或其他地方有它的属性吗?
我不想将序列化的表单数据发送到服务器(使用AJAX(。我想像普通的HTML操作一样提交带有未格式化数据的表单。
我在jQuery 中为此写了一个更好、更通用的破解方法
$('form').submit(function(){
var form = $(this);
$('input').each(function(i){
var self = $(this);
try{
var v = self.autoNumeric('get');
self.autoNumeric('destroy');
self.val(v);
}catch(err){
console.log("Not an autonumeric field: " + self.attr("name"));
}
});
return true;
});
此代码清除表单,并对非自动数值进行错误处理。
对于较新的版本,您可以使用选项:
unformatOnSubmit: true
在数据回调中,您必须调用getString方法,如下所示:
$("#form").autosave({
callbacks: {
data: function (options, $inputs, formData) {
return $("#form").autoNumeric("getString");
},
trigger: {
method: "interval",
options: {
interval: 300000
}
},
save: {
method: "ajax",
options: {
type: "POST",
url: '/Action',
success: function (data) {
}
}
}
}
});
使用get
方法。
'get'|通过".val(("或返回未格式化的对象".text(("|$(选择器(.autoNumeric('get'(;
<script type="text/javascript">
function clean(form) {
form["my_field"].value = "15";
}
</script>
<form method="post" action="submit.php" onsubmit="clean(this)">
<input type="text" name="my_field">
</form>
这将始终提交"15"
。现在发挥创意:(
镜像原始值:
<form method="post" action="submit.php">
<input type="text" name="my_field_formatted" id="my_field_formatted">
<input type="hidden" name="my_field" id="my_field_raw">
</form>
<script type="text/javascript">
$("#my_field_formatted").change(function () {
$("#my_field").val($("#my_field_formatted").autoNumeric("get"));
});
</script>
submit.php
中的忽略my_field_formatted
的值,而改用my_field
。
您可以始终使用php str_replace
函数
str_repalce(',','',$stringYouWantToFix);
它将删除所有逗号。如果需要,可以将该值强制转换为integer。
$("input.classname").autoNumeric('init',{your_options});
$('form').submit(function(){
var form=$(this);
$('form').find('input.classname').each(function(){
var self=$(this);
var v = self.autoNumeric('get');
// self.autoNumeric('destroy');
self.val(v);
});
});
classname是将初始化为autoNumeric的输入类抱歉英语不好^_^
还有另一种集成解决方案,它不会干扰客户端验证,也不会在提交前导致未格式化文本闪烁:
var input = $(selector);
var proxy = document.createElement('input');
proxy.type = 'text';
input.parent().prepend(proxy);
proxy = $(proxy);
proxy.autoNumeric('init', options);
proxy.autoNumeric('set', input.val())''
proxy.change(function () {
input.val(proxy.autoNumeric('get'));
});
您可以使用getArray方法(http://www.decorplanit.com/plugin/#getArrayAnchor)。
$.post("myScript.php", $('#mainFormData').autoNumeric('getArray'));
我想出了这个,似乎是最干净的方法。我知道这是一个相当古老的线程,但这是谷歌的第一个匹配,所以我会把它留在这里,供未来的使用
$('form').on('submit', function(){
$('.curr').each(function(){
$(this).autoNumeric('update', {aSign: '', aDec: '.', aSep: ''});;
});
});
AJAX用例解决方案
我相信这是上面提到的所有问题中更好的答案,因为写这个问题的人正在做AJAX。所以请投赞成票,这样人们就很容易找到它。对于非ajax表单提交,@jpaoletti给出的答案是正确的。
// Get a reference to any one of the AutoNumeric element on the form to be submitted
var element = AutoNumeric.getAutoNumericElement('#modifyQuantity');
// Unformat ALL elements belonging to the form that includes above element
// Note: Do not perform following in AJAX beforeSend event, it will not work
element.formUnformat();
$.ajax({
url: "<url>",
data : {
ids : ids,
orderType : $('#modifyOrderType').val(),
// Directly use val() for all AutoNumeric fields (they will be unformatted now)
quantity : $('#modifyQuantity').val(),
price : $('#modifyPrice').val(),
triggerPrice : $('#modifyTriggerPrice').val()
}
})
.always(function( ) {
// When AJAX is finished, re-apply formatting
element.formReformat();
});
autoNumeric("getArray")
不再工作。
当使用serializeArray((使用Ajax提交表单时,unformatOnSubmit: true
似乎不起作用。
相反,使用formArrayFormatted
来获得form.serializeArray()
的等效串行数据
只需从表单中获取任何AutoNumeric初始化的元素并调用该方法。它将串行化整个表单,包括非自动数字输入。
$.ajax({
type: "POST",
url: url,
data: AutoNumeric.getAutoNumericElement("#anyElement").formArrayFormatted(),
)};
- 如何删除除冒号、数字和'上午'或者'下午'
- 如何从字符串中删除除字母、数字、空格、感叹号和问号之外的所有内容
- 删除JS数组中的最小数字
- 删除图形和数字之间的连字符(-)符号
- 如何从此代码笔中删除数字和秒文本
- 正则表达式从字符串中删除数字(仅限年份)
- 从 javascript 中的 json 键中删除数字
- 选中/取消选中复选框时,在数组中添加/删除数字
- 从math.random中删除数字s
- 双波浪号(~~)运算符正在删除数字的小数部分
- 结合删除数字和下划线regex javascript
- 如何根据参数从数组中删除数字
- javascript中的正则表达式从url中删除数字路径
- 用于删除数字的正则表达式
- 删除数字(不是字符串)的最后数字
- 在Javascript中删除数字的前导零
- 为什么 javascript 正则表达式<>删除数字
- JavaScript 从字符串中删除数字 (N1,N2,..)
- Regex和Javascript从字符串中删除数字
- 在iMacros宏中使用Javascript删除数字中的空格和点's