如何检测输入文本字段中的更改并启用保存按钮

问题描述 投票:1回答:4

我正在尝试编写一个函数来检测文本字段中的更改并启用保存按钮。

这正在Visual Studio 2017,HTML和JavaScript中运行

(function () {


    var customer_address = localStorage.getItem("customer_address");
    var customer_city = localStorage.getItem("customer_city");
    var customer_postal_code = localStorage.getItem("customer_postal_code");



    //Address Section
    function changeAddress() {
        var oldaddress = $("#txtOldAddress").val();
        var newaddress = $("#txtNewAddress").val();


        var url = serverURL() + "/savenewaddress.php";

        var JSONObject = {
            "customer_username": localStorage.getItem("customer_username"),
            "oldaddress": oldaddress,
            "newaddress": newaddress,
        };

        $.ajax({
            url: url,
            type: 'GET',
            data: JSONObject,
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (arr) {
                _changeAddressResult(arr);
            },
            error: function () {
                alert("FAIL");
            }
        });
    }

    function _changeAddressResult(arr) {
        if (arr[0].result === 1) {
            /*localStorage.setItem($("#txtNewPassword").val(), customer_username);*/
            alert("OK!")
        }
        else {
            alert("FAIL2");
        }
    }

    //City Section
    function changeCity() {
        var oldcity = $("#txtOldCity").val();
        var newcity = $("#txtNewCity").val();

        var url = serverURL() + "/savenewcity.php";

        var JSONObject = {
            "customer_username": localStorage.getItem("customer_username"),
            "oldcity": oldcity,
            "newcity": newcity,
        };

        $.ajax({
            url: url,
            type: 'GET',
            data: JSONObject,
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (arr) {
                _changeAddressResult(arr);
            },
            error: function () {
                alert("FAIL");
            }
        });
    }

    function _changeAddressResult(arr) {
        if (arr[0].result === 1) {
            /*localStorage.setItem($("#txtNewPassword").val(), customer_username);*/
            alert("OK!")
        }
        else {
            alert("FAIL2");
        }
    }

    //Postal Code Section
    function changePostalCode() {
        var oldpostalcode = $("#txtOldPostalCode").val();
        var newpostalcode = $("#txtNewPostalCode").val();

        var url = serverURL() + "/savenewpostalcode.php";

        var JSONObject = {
            "customer_username": localStorage.getItem("customer_username"),
            "oldpostalcode": oldpostalcode,
            "newpostalcode": newpostalcode
        };

        $.ajax({
            url: url,
            type: 'GET',
            data: JSONObject,
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (arr) {
                _changeAddressResult(arr);
            },
            error: function () {
                alert("FAIL");
            }
        });
    }

    function _changeAddressResult(arr) {
        if (arr[0].result === 1) {
            /*localStorage.setItem($("#txtNewPassword").val(), customer_username);*/
            alert("OK!")

        }
        else {
            alert("FAIL2");
        }
    }


    //Validation
    $("#ChangeAddressForm").validate({
        rules: {
            txtNewPostalCode: {
                required: true,
                minlength: 6,
                digits: true
            },

            txtNewCity: {
               digits: false
            },
            txtNewAddress: {
                required: true,
                alphanumeric: true
            }

        },
        messages: {
            txtNewAddress: {
                required: "Address is required",
            },
            txtNewCity: {
                required: "City is required",
                digit: "Include only letters!",
            },
            txtNewPostalCode: {
                required: "Postal Code is required!",
                minlength: "Postal Code only contains 6 digits!",
                digits: "Postal Code only contains digits!"
            }
        },
        focusInvalid: false,
        submitHandler: function () {
            return false;
        },
        errorPlacement: function (error, element) {
            error.appendTo(element.parent().parent().after());
        },
    });



    //Save Address Button Function
    $("#btnSaveAddress").bind("click", function () {

        if ($("#ChangeAddressForm").valid()) {
            changeAddress();
            changeCity();
            changePostalCode();



        }
    });

})();

我希望3个字段中的文本框发生更改时,输出将启用提交按钮。如果没有更改,那么如果存在相同的文本,则该按钮将被禁用。

javascript jquery html json
4个回答
1
投票

尝试此代码:

function InputChangeListener(){
  flag = 1;

  var firstInput = document.getElementById("firstInput");
  var secondInput = document.getElementById("secondInput");
  var thirdInput = document.getElementById("thirdInput");

  var firstInputVal = document.getElementById("firstInput").defaultValue;
  var secondInputVal = document.getElementById("secondInput").defaultValue;
  var thirdInputVal = document.getElementById("thirdInput").defaultValue; 



  var submitButton = document.getElementById("submitButton");
   if(firstInput.value == firstInputVal || secondInput.value == secondInputVal || thirdInput.value == thirdInputVal){
       flag = 0;
   }
   if(flag == 1){
      submitButton.style.display = "block"; 
   } else {
      submitButton.style.display = "none"; 
   }
}
#submitButton{
display: none
}
<input id="firstInput" value="test1" type="text" onkeyup ="InputChangeListener()"/><br />
<input id="secondInput" value="test2" type="text" onkeyup ="InputChangeListener()"/><br />
<input id="thirdInput" value="test3" type="text" onkeyup ="InputChangeListener()"/><br />
<input id="submitButton" type="submit" value="Submit" />

0
投票

您可以具有一个在每次输入更改时都会调用的功能,然后检查是否输入了所有输入。

示例:

<input id = "input1" onchange ="checkIfAllInputsEntered()"/>
<input id = "input2" onchange ="checkIfAllInputsEntered()"/>
<input id = "input3" onchange ="checkIfAllInputsEntered()"/>

JavaScript:

function checkIfAllInputsEntered(){
  flag = 1;
  //get all 3 elements by id's and check their values if they are "" or not
   if(input1 == ""){
       flag = 0;
   }
   if(input2 == ""){
       flag = 0;
   }
   if(input3 == ""){
       flag = 0;
   }

   if(flag == 1)
      //enable button

}

0
投票

通常仅在输入更改焦点后才触发Onchange。因此,您需要使用onkeyup事件。但是您也可以添加onchange和onkeydown以防万一。我假设您正在使用jQuery。这是示例:


var old1;
var old2;
var old3;

function checkForChange() {
  if (
    old1 != $('#in1').val() ||
    old2 != $('#in2').val() ||
    old3 != $('#in3').val()
  ) {
    $('#save').show();
  }
}
$(function() {
  old1 = $('#in1').val();
  old2 = $('#in2').val();
  old3 = $('#in3').val();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
  <input id="in1" onchange="checkForChange()" onkeydown="checkForChange()" onkeyup="checkForChange()" value="hello world" /><br/>
  <input id="in2" onchange="checkForChange()" onkeydown="checkForChange()" onkeyup="checkForChange()" value="hello cat" /><br/>
  <input id="in3" onchange="checkForChange()" onkeydown="checkForChange()" onkeyup="checkForChange()" value="hello dog" /><br/>
  <input id="save" value="save" type="submit" style="display:none;" />
</form>

-1
投票

我正在尝试编写一个可以检测文本变化的函数字段并启用保存按钮。

首先,在您的按钮标签中添加“已禁用”属性。然后在您的按钮中添加一个“ onchange”事件。在var old1; var old2; var old3; function checkForChange() { if ( old1 != $('#in1').val() || old2 != $('#in2').val() || old3 != $('#in3').val() ) { $('#save').show(); } } $(function() { old1 = $('#in1').val(); old2 = $('#in2').val(); old3 = $('#in3').val(); });中执行以下操作:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<form>
  <input id="in1" onchange="checkForChange()" onkeydown="checkForChange()" onkeyup="checkForChange()" value="hello world" /><br/>
  <input id="in2" onchange="checkForChange()" onkeydown="checkForChange()" onkeyup="checkForChange()" value="hello cat" /><br/>
  <input id="in3" onchange="checkForChange()" onkeydown="checkForChange()" onkeyup="checkForChange()" value="hello dog" /><br/>
  <input id="save" value="save" type="submit" style="display:none;" />
</form>
© www.soinside.com 2019 - 2024. All rights reserved.