admin管理员组

文章数量:1387360

I have couple of input field and values in them. This is projected to the user. The user can modify these values and submit them. When submitted, I need to check which input field is modified.

I can pare the previous fields and current fields and check. But I am trying to find more optimized way to do this. I can use javascript, php, jquery and html tricks

I have couple of input field and values in them. This is projected to the user. The user can modify these values and submit them. When submitted, I need to check which input field is modified.

I can pare the previous fields and current fields and check. But I am trying to find more optimized way to do this. I can use javascript, php, jquery and html tricks

Share Improve this question asked May 9, 2013 at 18:08 JithuJithu 1113 silver badges11 bronze badges 2
  • 1 what do you exactly mean by modified? since when? do you mean different from the database-stored value or something else? – İsmet Alkan Commented May 9, 2013 at 18:12
  • ^^^ +1 Do you mean client side change or changing data in the DB? – ExceptionLimeCat Commented May 9, 2013 at 18:19
Add a ment  | 

5 Answers 5

Reset to default 6
<input id="input1" value="someValue" type="text">
<input id="input2" value="someValue" type="text">

Script:

$('input').on('change',function(){
  var id = $(this).attr('id');
  alert("input field is modified : ID = " + id);
});

You can create 2 different input, 1 hidden with a class like originalVal and 1 visible for every input.

Then on submit you do something like that :

$('input').each(function(){
     var currentVal = $(this).val();
     var originalVal = $(this).closest('.originalVal').val()
     if(currentVal  != originalVal){
          //This input has changed
     }
})

Since no code was given, you could pare what was in the input pared to what is now in it.

HTML Input:

<input type="text" id="testInput" value="DB Value"/>

jQuery

var modifiedInputs = [];
var oldVal = "";

$("#testInput").focus(function() {
    oldVal = this.value;
}).blur(function() {
    console.log("Old value: " + oldVal + ". New value: " + this.value);

    //If different value, add to array:
    if (this.value != oldVal) {
        modifiedInputs.push(this.id);
    }
});

Fiddle: http://jsfiddle/tymeJV/tfmVk/1/

Edit: Took it a step further, on modification of an input, if the changed value is different from the original, it pushes the elements ID to the array.

I would say your best bet would be to get the initial values from the input fields, and then pare them later on. Then, just do a parison once the submit button is clicked. For instance, put this somewhere in your $(document).ready() that way it will retrieve the initial value.

var oldValue=[];
$('input').each(function(){
    oldValue.push($(this).val());
});

Then you can pare later on when you hit the submit.

you could pare with default value like this

for(var i in formObj)
    if('value' in formObj[i] && formObj[i].value!=formObj[i].defaultValue){
        //do what ever here ...
    }

本文标签: phpHow to find which input field is modifiedStack Overflow