JQuery的。将输入占位符设置为在经过1秒后输入到输入的最后一个值。怎么样?

时间:2017-08-07 04:25:09

标签: javascript jquery

基本上我需要将输入占位符设置为在经过1秒后输入输入的最后一个值。原因是,如果用户再次从中删除了所有内容,则占位符现在将显示在最后一次自动调用AJAX调用之后键入输入的最后一个值,而不是在原始页面加载期间加载的占位符值

工作代码

  function autoSave(client_id, project_id, mainsheet_id, t_id, t_val) {

    if (t_val != '') {

      // Refresh variables just in case.
      client_id = $('#CLIENT_ID').val();
        project_id = $('#PROJECT_ID').val();
        mainsheet_id = $('#MAINSHEET_ID').val();

            // AJAX POST request to run MySQL UPDATE query on this database field ( WTRESRVD ).
            $.ajax({
                url: "processor.php",
                method: "GET",
                data: {
                    postCLIENT_ID: client_id,
                    postPROJECT_ID: project_id,
                    postMAINSHEET_ID: mainsheet_id,
                    postT_ID: t_id,
                    postT_VAL: t_val
                },
                dataType: "text",
                beforeSend: function() {
                    // setting a timeout
                    $('#status').text('Please wait...');

                },

                success: function(data) {
                    // If data return after a successful request isn't an empty string..
                  // ADD SERVER RESPONSE HANDING HERE.

                  /* Status Codes

                  return 0 = Nothing to Update
                  return 1 = Successful Update Query
                  return 2 = Database Connection refused
                  return 3 = MySQL Query Error OR Wrong URL Parameters */

                  // If data return 0 = Nothing to Update
                  if (data == '0') {

                  $('#status').text("Status: Nothing changed. Nothing saved.").show();
                  // ..fadeOut over 3 seconds. 
                  $('#status').fadeOut(5000);

                  }

                  // If data return 1 = Successful Update Query
                  if (data == '1') {

                  // Create variable time to reference later.
                  var time = showTime();
                  // Update div status with last saved time stamp then..
                  $('#status').text("Status: Saved @ " + time).show();
                  // ..fadeOut over 3 seconds. 
                  $('#status').fadeOut(5000);

                  }

                  // return 2 = Database Connection refused
                  if (data == '2') {

                  $('#status').text("Status: Database Connection refused.").show();
                  // ..fadeOut over 3 seconds. 
                  $('#status').fadeOut(5000);

                  }

                  // return 3 = MySQL Query Error OR Wrong URL Parameters
                  if (data == '3') {

                  $('#status').text("Status: MySQL Query Error OR Wrong URL Parameters.").show();
                  // ..fadeOut over 3 seconds. 
                  $('#status').fadeOut(5000);

                  }

                }

            });
        } else {

        // like this maybe???
        // not working...
        $(t_val).attr("placeholder", t_val);

        }

  }

1 个答案:

答案 0 :(得分:0)

您可以使用.queue().delay()在N秒内执行任务

$("#selector").delay(1000, "placeholder").queue("placeholder", function() {
  $(this).prop("placeholder", "value" /* set placeholder value here */);
}).dequeue("placeholder");