jQuery 如何在提交表单之前删除 AutoNumeric 格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15815771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I remove AutoNumeric formatting before submitting form?
提问by zeal
I'm using the jQuery plugin AutoNumericbut when I submit a form, I can't remove the formatting on the fields before POST
.
我正在使用 jQuery 插件AutoNumeric但是当我提交表单时,我无法删除POST
.
I tried to use $('input').autonumeric('destroy')
(and other methods) but it leaves the formatting on the text fields.
我尝试使用$('input').autonumeric('destroy')
(和其他方法),但它在文本字段上留下了格式。
How can I POST
the unformatted data to the server? How can I remove the formatting? Is there an attribute for it in the initial config, or somewhere else?
我怎样才能POST
将未格式化的数据传送到服务器?如何删除格式?在初始配置或其他地方是否有它的属性?
I don't want to send the serialized form data to the server (with AJAX). I want to submit the form with the unformatted data like a normal HTML action.
我不想将序列化的表单数据发送到服务器(使用 AJAX)。我想像普通的 HTML 操作一样提交带有未格式化数据的表单。
采纳答案by zeal
I wrote a better, somewhat more general hack for this in jQuery
我在 jQuery 中为此编写了一个更好的、更通用的 hack
$('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;
});
This code cleans form w/ error handling on not autoNumeric values.
此代码清除带有非 autoNumeric 值的错误处理的表单。
回答by jpaoletti
With newer versions you can use the option:
对于较新的版本,您可以使用以下选项:
unformatOnSubmit: true
回答by ararog
Inside data callback you must call getString method like below:
在数据回调中,您必须调用 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) {
}
}
}
}
});
回答by Alireza A. Ahmadi
You can always use php str_replace
function
您可以随时使用 phpstr_replace
函数
str_repalce(',','',$stringYouWantToFix);
it will remove all commas. you can cast the value to integer if necessary.
它将删除所有逗号。如有必要,您可以将该值转换为整数。
回答by Halcyon
Use the get
method.
使用get
方法。
'get' | returns un-formatted object via ".val()" or ".text()" | $(selector).autoNumeric('get');
'得到' | 通过“.val()”或“.text()”返回未格式化的对象| $(selector).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>
This will always submit "15"
. Now get creative :)
这将始终提交"15"
。现在发挥创意:)
Mirrored raw value:
镜像原始值:
<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>
The in submit.php
ignore the value for my_field_formatted
and use my_field
instead.
insubmit.php
忽略值my_field_formatted
并使用my_field
代替。
回答by Asdar Syam
$("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);
});
});
classnameis your input class that will init as autoNumeric Sorry for bad English ^_^
classname是您的输入类,它将初始化为 autoNumeric 抱歉英语不好^_^
回答by Jan Drábek
There is another solution for integration which doesn't interfere with your client-side validation nor causes the flash of unformatted text before submission:
还有另一种集成解决方案,它不会干扰您的客户端验证,也不会在提交之前导致无格式文本的闪烁:
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'));
});
回答by Alin A
You could use the getArraymethod (http://www.decorplanit.com/plugin/#getArrayAnchor).
您可以使用getArray方法(http://www.decorplanit.com/plugin/#getArrayAnchor)。
$.post("myScript.php", $('#mainFormData').autoNumeric('getArray'));
$.post("myScript.php", $('#mainFormData').autoNumeric('getArray'));
回答by Piotrek
I came up with this, seems like the cleanest way. I know it's a pretty old thread but it's the first Google match, so i'll leave it here for future
我想出了这个,似乎是最干净的方法。我知道这是一个很旧的线程,但它是第一个谷歌匹配,所以我会把它留在这里以备将来使用
$('form').on('submit', function(){
$('.curr').each(function(){
$(this).autoNumeric('update', {aSign: '', aDec: '.', aSep: ''});;
});
});