简体   繁体   English

jQuery:如何在 ajax 调用中获取输入值?

[英]jQuery: How to get input value within an ajax call?

I have a jQuery script, in which I need to load data from a CSV from an external URL.我有一个 jQuery 脚本,我需要在其中从外部 URL 加载来自 CSV 的数据。

At the same time, I need to combine the data from the CSV with data provided by a frontend user through an input field.同时,我需要将来自 CSV 的数据与前端用户通过输入字段提供的数据结合起来。 My expected result would be that I'd be able to call the jQuery code for fetching the value from the user's entry into the input field.我的预期结果是我将能够调用 jQuery 代码以从用户输入到输入字段中获取值。 However, while this works when the code is placed outside the ajax call, it does not work inside.然而,虽然当代码放在 ajax 调用之外时这有效,但它在内部不起作用。

At the same time, I can't place the code for fetching the user's input outside the ajax call, as I need to be able to utilize information from the loaded CSV together with the user input to perform a dynamic calculation.同时,我不能将用于获取用户输入的代码放在 ajax 调用之外,因为我需要能够利用来自加载的 CSV 的信息和用户输入来执行动态计算。

My code is as below:我的代码如下:

HTML: HTML:

<input type="text" id="my_input" value="12" maxlength="2">

Javascript: Javascript:

$.ajax({
    url: csv_url,
    async: false,
    success: function (csvd) {
      var csv = $.csv.toObjects(csvd);
      // XYZ done with the CSV data to populate various fields

      $("#my_input").keyup(function () {
          console.log(this.value);
          // this does not result in anything being logged
      });
    },
    dataType: "text",
    complete: function () {}
  });

Of course it will not work.当然是行不通的。 You are telling the compiler to add the keyup event listener to input after the ajax call is successful.您是在告诉编译器在 ajax 调用成功后将 keyup 事件侦听器添加到 input 中。 Which means it will not work until ajax call is completed successfully.这意味着在 ajax 调用成功完成之前它不会工作。

You need to put the keyup event listener outside and inside the ajax call just get the value like below:您需要将 keyup 事件侦听器放在 ajax 调用的外部和内部,只需获取如下值:

let myInputValue = "";

    $("#my_input").keyup(function () {
        console.log(this.value);
        myInputValue  = this.value;

     });
    
    $.ajax({
        url: csv_url,
        async: false,
        success: function (csvd) {
          var csv = $.csv.toObjects(csvd);
          // XYZ done with the CSV data to populate various fields
    
          //And later use the myInputValue here
        },
        dataType: "text",
        complete: function () {}
      });

You have not give us enough info.你没有给我们足够的信息。 if you only need the current value in the ajax call you can do like below:如果您只需要 ajax 调用中的当前值,您可以执行以下操作:

$.ajax({
            url: csv_url,
            async: false,
            success: function (csvd) {
              var csv = $.csv.toObjects(csvd);
              // XYZ done with the CSV data to populate various fields
        
              let myInputValue = $("#my_input").val();//And later use the myInputValue here
            },
            dataType: "text",
            complete: function () {}
          });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM