简体   繁体   English

如何从选择输入字段接收和显示值并将其发送到数据库

[英]How to receive and display and send values from Select input field to database

This a sample of the dropdowns using select that I have这是使用我选择的下拉列表的示例

<label class="form__label" for="country"> Country Of Residence</label>
<select id="country" class="form__input" name="country"/>
    <option value="null">Select Country</option>
    <option value="United Arab Emirates">United Arab Emirates</option>
    <option value="Bahrain">Bahrain</option>
    <option value="Kuwait">Kuwait</option>
    <option value="Oman">Oman</option>
</select>

The value is stored in the database as a 'String'.该值作为“字符串”存储在数据库中。

I would appreciate some help in understanding the best way forward for 2 things我很感激在理解两件事的最佳前进方向方面的帮助

On Load负载

The string value from the database should be the option displayed in my dropdown.数据库中的字符串值应该是我的下拉列表中显示的选项。 And if for some reason the string value in the database does not match, then the 'Select Country' option should be displayed.如果由于某种原因数据库中的字符串值不匹配,则应显示“选择国家/地区”选项。

On Change更改时

The selected value should be the value that's sent to the database as a String.选定的值应该是作为字符串发送到数据库的值。 The functionality for this is already implemented but earlier I was using a input of type=text .. So what type of changes are needed to send this value now from a select field.这个功能已经实现了,但早些时候我使用了type=textinput ..那么现在从选择字段发送这个值需要什么类型的更改。

I've researched on the net but the more I research the more I get confused.我在网上研究过,但我研究得越多,我就越困惑。 And most answers seem to be jQuery solutions.大多数答案似乎都是 jQuery 解决方案。 I am looking for some help with Vanilla Javascript.我正在寻找有关 Vanilla Javascript 的帮助。 Somethings I need to get clarity on is 'Do I need to have a hidden field to store the value and send and receive from database?'我需要弄清楚的是“我是否需要一个隐藏字段来存储值并从数据库发送和接收?” .. I am really confused with the information I've researched. .. 我真的对我研究过的信息感到困惑。

Any help would be appreciated.任何帮助,将不胜感激。

Get select element value on event using pure JavaScript, Try this and get value.使用纯 JavaScript 获取事件的选择元素值,试试这个并获取值。

var select_element = document.getElementById('country');
    
select_element.onchange = function() {
    var elem = (typeof this.selectedIndex === "undefined" ? window.event.srcElement : this);
    var value = elem.value || elem.options[elem.selectedIndex].value;
    alert(value);
    // your another required code set!
}​
    

After a couple of days of struggling, I am using this solution.经过几天的挣扎,我正在使用此解决方案。 If this is overkill and there's a simpler approach, please do share in comments.如果这有点矫枉过正并且有更简单的方法,请在评论中分享。

So I first identified the select element所以我首先确定了select元素

const country = document.getElementById('country');

Then created a function as below然后创建一个函数如下

const displayCountry = function() {
  //CREATE ARRAY OF ALL OPTIONS
  const optionsArray = Object.entries(country.children);
  console.log(optionsArray);
  // GET NAME OF COUNTRY FROM SELECT FIELD
  const nameOfCountry = country.getAttribute("value");
  console.log(nameOfCountry);
  // FIND OPTION WITH THE SAME COUNTRY NAME
  const option = optionsArray.find(
    (option) => option[1].value === nameOfCountry
  );
  // UPDATE OPTION FIELD BY SETTING ATTRIBUTE OF SELECTED
  option[1].setAttribute("selected", true);
  console.log(option[1]);
};

It can be shortened as below它可以缩短如下

const displayCountryShortened = function() {
  Object.entries(country.children)
    .find((option) => option[1].value === country.getAttribute("value"))[1]
    .setAttribute("selected", true);
};

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

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