英文:
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 :
- adress: ""
 - companyName: ""
 - contactName: ""
 - 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 :
- 
adress: ""
 - 
companyName: ""
 - 
contactName: ""
 - 
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.
答案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('click', e => {
  let newCompany = {
    companyName: inputCompanyName.value,
    contactName: inputContactName.value,
    contactTitle: inputContactTitle.value,
    adress: country.value,
  };
  // the rest of the click handler logic...
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论