使用文本区域和 select 标签将 json 数据发送到 google 表单

Sending json data to google form with textarea and select tag

我创建了一个应用程序,它使用 ajax 将 json 数据发送到 google 表单。我的问题是我无法发送我想要的所有数据,这意味着我无法在 textarea 和 select 标签内发送一些内容。我正在使用 url,不幸的是,我不得不使用一些隐藏的内容来将一些数据从一个页面移动到另一个页面,如果我不这样做,我将无法获得一些数据。

这是我的代码: javascript:

$( document ).ready(function() {

function getParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, "\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
    results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
  }

  function postContactToGoogle() {
    var full_name = getParameterByName('full_name');
    var home_address = getParameterByName('home_address');
    var email = getParameterByName('email');
    var phone_number = getParameterByName('phone_number');
    var condition = getParameterByName('condition');
    var extra = getParameterByName('extra');
    var when_to_sell = getParameterByName('when_to_sell');
    console.log(full_name, home_address, email, phone_number, condition, extra, when_to_sell);
    var google_url = "https://docs.google.com/forms/d/e/1FAIpQLSdqMQL_dzhdFSVpuVtlcfIa4xHe9DNP8yozB1ncAB2e_byBJQ/formResponse";

    if (full_name !== "" || email !== "" || phone_number !== "" || home_address !== "" || condition !== "" || extra !== "" || when_to_sell !== "")
    {
      $.ajax({
        url: google_url,
        data: { 
          "entry.515947838": full_name,
          "entry.190795925": email,
          "entry.1306603567": phone_number,
          "entry.1032461271": home_address,
          "entry.100378601": condition,
          "entry.637314286": extra,
          "entry.608122467": when_to_sell
        },
        type: "POST",
        dataType: "json",
        statusCode: {
          0: function () {
          },
          200: function () {
          }
        }
      });
    }
  }
  postContactToGoogle();
});

这里的 html 给我带来了很多问题:这是我收集一些数据的页面,稍后我将使用 ajax

发送这些数据
<form action="step-3.html" id="form-step2">
        <div class="form-control full-width">
          <label for="condition">What conditions is the property in?</label>
          <select id="condition" name="condition">
            <option value="Good">Good</option>
            <option value="Fair">Fair</option>
            <option value="Bad">Bad</option>
          </select>
        </div>
        <div class="form-control full-width">
          <label for="extra">Any updates, amenities or extras?</label> 
          <textarea name="extra" id="extra" placeholder="e.g: recent reno, pool etc." rows="3"></textarea>
        </div>
        <div class="form-control full-width">
          <label for="when_to_sell">When do you want to sell?</label>
          <select id="when_to_sell" name="when_to_sell">
            <option value="Immediately">Immediately</option>
            <option value="Next 1-3 months">Next 1-3 months</option>
            <option value="Next 3-6 months">Next 3-6 months</option> 
          </select>
        </div>
        <div class="form-control full-width">
          <label>Are you currently working with a Real Estate Agent?*</label>
          <input checked="checked" id="working_for_real_estate_agent_no" name="working_for_real_estate_agent" type="radio" />
          <label class="radio-label" for="working_for_real_estate_agent_no">No</label>
          <input id="working_for_real_estate_agent_yes" name="working_for_real_estate_agent" type="radio" />
          <label class="radio-label" for="working_for_real_estate_agent_yes">Yes, I am working with a Realtor.</label>
          <div class="asteriks">*We ask this to ensure there is no conflict of interest.</div>
        </div>
        <div class="form-control full-width">
          <input id="home_address" name="home_address" type="hidden" />
          <div class="home-evaluation-option-container">
            <div class="home-evaluation-option">
              <input id="exact-market-value" name="exact-market-value" type="button" value="Send Property Details" />
            </div>
          </div>
        </div>
      </form>

这是我尝试过的技巧,不幸的是它只适用于输入标签:

<form action="step-4.html" id="form-step2">
        <div class="form-control full-width">
          <input name="full_name" placeholder="Full Name" type="input" data-validation="length alphanumeric" data-validation-length="min1" data-validation-error-msg="Please, insert your name" />
        </div>
        <div class="form-control full-width">
          <input id="email" name="email" placeholder="Email" type="input" data-validation="email" />
        </div>
        <div class="form-control full-width">
          <input id="phone_number" name="phone_number" placeholder="Phone Number" type="input" name="home-phone" data-validation="number"  />
        </div>

        <input id="home_address" name="home_address" type="hidden" class="hide" />

        <select id="condition" name="condition" class="hide">
          <option value="Good">Good</option>
          <option value="Fair">Fair</option>
          <option value="Bad">Bad</option>
        </select>
        <textarea name="extra" id="extra" class="hide"></textarea>
        <select id="when_to_sell" name="when_to_sell" class="hide">
          <option value="Immediately">Immediately</option>
          <option value="Next 1-3 months">Next 1-3 months</option>
          <option value="Next 3-6 months">Next 3-6 months</option> 
        </select>

        <div class="form-control submit-button">
          <input name="submit" type="submit" value="Generate Report" />
        </div>
      </form>

想过从 select 或文本区域收集数据吗?

这是您可以找到实际代码的页面:

http://homeworth.online/hockinghomesteam/

这是一个如何从文本区域元素中获取值的示例:

<!DOCTYPE html>
<html>
<body>

<textarea id="idExtra_Info">
Some content
</textarea>

<button type="button" onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var x = document.getElementById("idExtra_Info").value;
    alert(x);
}
</script>

</body>
</html>