简体   繁体   English

如何通过数据属性或文本设置下拉选择?

[英]How to set dropdown select through data-attribute or text?

I am working on a JavaScript function which should copy shipping address form values to a billing address form section in case both are identical. 我正在使用JavaScript函数,如果两者相同,则应将送货地址表格值复制到帐单地址表格部分。

I wanted to rely either on a data attribute, text or innerHTML to set the select, since option id changes once a selection has been made. 我想依靠数据属性,文本或innerHTML来设置选择,因为一旦做出选择,选项ID就会更改。

<select id="order_bill_to_land" name="order[bill_to_land_id]"><option value="">select a country</option>
  <option value="1" data-land="1">Afghanistan</option>
  <option value="2" data-land="2">Aland Islands</option>
  <option value="3" data-land="3">Albania</option>

My JavaScript function is: 我的JavaScript函数是:

let land = document.querySelector("#country").getAttribute('data-country');
let bl = document.querySelector("#order_bill_to_land");

for (let i = 0; i < bl.options.length; i++) {
  if (bl.options[i].text === land) {
    return console.log("if loop working");
    bl.selectedIndex = i;
    break;
  };
}

Which produces 0 , because the if conditional does not work which I deduce from the fact that I am unable to log to console. 产生0 ,因为如果if条件不起作用,这是由于我无法登录控制台这一事实得出的。

Why is that so? 为什么会这样?

I found a shorter version for selecting the selectedIndex: 我发现了用于选择selectedIndex的较短版本:

bl.selectedIndex = [...bl.options].findIndex (option => option.innerHTML === land);

but it produces -1 . 但它产生-1 Since it is so nicely condensed I am unable to find out why it does not work. 由于它是如此简洁,我无法找出为什么它不起作用。

How can I get this working? 我该如何工作?

Thank you in advance! 先感谢您!

Update 更新

The for loop works, since I was able to log all options of bl by putting a console.log outside of the if statement. for循环有效,因为我可以通过将console.log放在if语句之外来记录bl的所有选项。 This is what I did: 这是我所做的:

for (let i = 0; i < bl.options.length; i++) {
  console.log(land);                            // Line 185
  console.log(bl.options[i].text);              // Line 186
  if (bl.options[i].text === land) {            // Line 187
    bl.selectedIndex = i;
    break;
  };
}

console.log(bl.selectedIndex);                  // Line 203

Which produces the following console output: 产生以下控制台输出:

...
[Log] Germany (dev.js, line 185)
[Log] Zambia (dev.js, line 186)
[Log] Germany (dev.js, line 185)
[Log] Zimbabwe (dev.js, line 186)
[Log] 0 (dev.js, line 203)

In debug I can check bl , options[i] and land in line 187, but not .text . 在调试中,我可以检查bloptions[i]并在第187行land ,但不能检查.text I do not know if this is normal. 我不知道这是否正常。 Since I am able to log them outside of the if-statement this should work, or? 由于我能够将它们记录在if语句之外,因此应该可以工作,或者?

Theoretically it should loop through bl and when bl.options[i].text (or .innerHTML ) matches land break the loop and output bl.selectedIndex = i and therefor set the select form. 理论上,它应该遍历bl并且当bl.options[i].text (或.innerHTML )匹配时, land中断循环并输出bl.selectedIndex = i ,从而设置选择形式。

Update 2 更新2

In console I get: 在控制台中,我得到:

let bl = document.querySelector("#order_bill_to_land")
undefined
let land = document.querySelector("#country").getAttribute('data-country');
undefined
land;
"Germany"
bl.options[84].text
"Germany"

So both are type String, but when I test: 所以两者都是String类型,但是当我测试时:

land === bl.options[84].text;

I get false . false

I suspect land and bl.options[i].text end up being different object types and following developer.mozilla.org on "Using the equality operators" . 我怀疑landbl.options[i].text最终会成为不同的对象类型,并遵循developer.mozilla.org的“使用相等运算符” This generates a runtime error. 这会生成运行时错误。 I do not know yet how to solve it. 我还不知道如何解决。

Thanks to @aseferov I found the problem: 感谢@aseferov我发现了问题:

land had a tailing invisible character which was produced by ruby on the server through: land有一个红宝石,它是由服务器上的红宝石通过以下方式产生的:

data-country="<%= @order.land.name %>"

Which produced 哪个产生

let land = document.querySelector("#country").getAttribute('data-country');

After land.trim() the conditional worked. 在land.trim()之后,条件条件起作用了。

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

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