getElementById 返回空值,因此发布空值。

huangapple go评论58阅读模式
英文:

getElementById return empty values so post empty values

问题

以下是您要翻译的内容:

I'm working with DOM and web API to POST some information about the company like name, worker's name.

But when I write something in the input DOM can't reach the value and return empty so I post an empty object.

That looks like :

  1. adress: ""
  2. companyName: ""
  3. contactName: ""
  4. contactTitle: ""

My form block:

<form>
        <div class="form-group">
          <label for="">Company Name</label>
          <input
            type="text"
            class="form-control"
            id="companyName"
            placeholder="Company Name!"
          />
        </div>
        <div class="form-group">
          <label for="">Contact Name</label>
          <input
            type="text"
            class="form-control"
            id="contactName"
            placeholder="Contact Name!"
            value=""
          />
        </div>
        <div class="form-group">
          <label for="">Contact Title</label>
          <input
            type="text"
            class="form-control"
            id="contactTitle"
            placeholder="Contact Title!"
          />
        </div>
        <div class="form-group">
          <label for="">Country</label>
          <input
            type="text"
            class="form-control"
            id="inputCountry"
            placeholder="Country!"
          />
        </div>
      </form>

And my JS code:

'use strict';

let inputCompanyName = document.getElementById('companyName');
let inputContactName = document.getElementById('contactName');
let inputContactTitle = document.getElementById('contactTitle');
let country = document.getElementById('inputCountry');

const btnSubmit = document.getElementById('submit');

let newCompany = {
  companyName: inputCompanyName.value,
  contactName: inputContactName.value,
  contactTitle: inputContactTitle.value,
  adress: country.value,
};

btnSubmit.addEventListener('click', e => {
  e.preventDefault();
  axios
    .post('https://northwind.vercel.app/api/suppliers', newCompany)
    .then(res => {
      console.log('Response', res.data);
      alert('Success!');
    });
});

I tried innerHTML and innerText and form method but I cant solve this problem.

英文:

I'm working with DOM and web API to POST some information about the company like name, worker's name.

But when I write something in the input DOM can't reach the value and return empty so I post an empty object.

That looks like :

  1. adress: ""

  2. companyName: ""

  3. contactName: ""

  4. contactTitle: ""

My form block:

&lt;form&gt;
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;&quot;&gt;Company Name&lt;/label&gt;
          &lt;input
            type=&quot;text&quot;
            class=&quot;form-control&quot;
            id=&quot;companyName&quot;
            placeholder=&quot;Company Name!&quot;
          /&gt;
        &lt;/div&gt;
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;&quot;&gt;Contact Name&lt;/label&gt;
          &lt;input
            type=&quot;text&quot;
            class=&quot;form-control&quot;
            id=&quot;contactName&quot;
            placeholder=&quot;Contact Name!&quot;
            value=&quot;&quot;
          /&gt;
        &lt;/div&gt;
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;&quot;&gt;Contact Title&lt;/label&gt;
          &lt;input
            type=&quot;text&quot;
            class=&quot;form-control&quot;
            id=&quot;contactTitle&quot;
            placeholder=&quot;Contact Title!&quot;
          /&gt;
        &lt;/div&gt;
        &lt;div class=&quot;form-group&quot;&gt;
          &lt;label for=&quot;&quot;&gt;Country&lt;/label&gt;
          &lt;input
            type=&quot;text&quot;
            class=&quot;form-control&quot;
            id=&quot;inputCountry&quot;
            placeholder=&quot;Country!&quot;
          /&gt;
        &lt;/div&gt;
      &lt;/form&gt;

And my JS code:

&#39;use strict&#39;;

let inputCompanyName = document.getElementById(&#39;companyName&#39;);
let inputContactName = document.getElementById(&#39;contactName&#39;);
let inputContactTitle = document.getElementById(&#39;contactTitle&#39;);
let country = document.getElementById(&#39;inputCountry&#39;);

const btnSubmit = document.getElementById(&#39;submit&#39;);

let newCompany = {
  companyName: inputCompanyName.value,
  contactName: inputContactName.value,
  contactTitle: inputContactTitle.value,
  adress: country.value,
};

btnSubmit.addEventListener(&#39;click&#39;, e =&gt; {
  e.preventDefault();
  axios
    .post(&#39;https://northwind.vercel.app/api/suppliers&#39;, newCompany)
    .then(res =&gt; {
      console.log(&#39;Response&#39;, res.data);
      alert(&#39;Success!&#39;);
    });
});

I tried innerHTML and innerText and form method but I cant solve this problem.

答案1

得分: 3

你正在加载页面时立即读取值,远在用户有机会输入任何值之前。

而是在单击事件中读取这些值:

btnSubmit.addEventListener('click', e => {
  let newCompany = {
    companyName: inputCompanyName.value,
    contactName: inputContactName.value,
    contactTitle: inputContactTitle.value,
    address: country.value,
  };
  // 其余的点击处理逻辑...
});
英文:

You're reading the values immediately upon loading the page, long before the user has had a chance to enter any values.

Instead, read the values in the click event:

btnSubmit.addEventListener(&#39;click&#39;, e =&gt; {
  let newCompany = {
    companyName: inputCompanyName.value,
    contactName: inputContactName.value,
    contactTitle: inputContactTitle.value,
    adress: country.value,
  };
  // the rest of the click handler logic...
});

huangapple
  • 本文由 发表于 2023年2月14日 05:43:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定