仅从已更改的文本输入中收集值 [英] collect value only from the text input that have been changed

查看:91
本文介绍了仅从已更改的文本输入中收集值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个html页面,在ajax调用之后显示很多数据行,在每一行中我都输入了具有不同值的文本,用户可以编辑此文本输入,然后按下按钮来执行一些计算动作。 p>

如何使用javascript(jQuery)收集只有已更改的这些值,并将它们发送到php脚本。我搜索一个简单的方法,而不是保存所有大数组的旧值,然后与新值进行比较,是否有这样的解决方案?

解决方案

您可以使用 .data() 函数保留其原始值的副本,然后在提交时将其与其值进行比较以确保仅提交更改的值。

  var inputs = $('input [type =text]')。each(function(){
$(this).data('original',this.value);
}); ($(this)).data('original')。
$ b $('#form')。submit(function(){
inputs.each )!== this.value){
//为变化的值做些事
}其他$ {
//其他的东西
}
});
});

请参阅此处的演示: http://www.jsfiddle.net/yijiang/twCE9/


I have a html page that after ajax call show a lot of data rows, in every row i have input text with different value, the user can edit this text input and then press a button to do some calculate action.

How can I collect with javascript(jQuery) only this values that have been changed, and send them to php script. I search a simple way, not to save all big array of old values then compare with new values, Is there such solution?

解决方案

You can use the .data() function to keep a copy of their original values, then compare that against their values when submitting to make sure that only changed values are submitted.

var inputs = $('input[type="text"]').each(function() {
    $(this).data('original', this.value);
});

$('#form').submit(function(){
    inputs.each(function() {
        if ($(this).data('original') !== this.value) {
            // Do something for the changed value
        } else {
            // And something else for the rest.
        }
    });
});

See a demo of this here: http://www.jsfiddle.net/yijiang/twCE9/

这篇关于仅从已更改的文本输入中收集值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆