简体   繁体   English

根据相关文本选择下拉选项

[英]select drop down option based on associated text

I have a drop down with a value and associated text as such: 我有一个值和相关文本的下拉列表,例如:

echo '<option value="'.$row['company_id'].'">'.$row['company_name'].'</option>';

Eventually, the output of the selected option is put into a table, row by row. 最终,所选选项的输出逐行放入表中。 By each row, there is an edit button to edit that particular row and select a new drop down option. 每行都有一个编辑按钮,用于编辑该特定行并选择一个新的下拉选项。 I'm trying to use JavaScript to select the text and when they hit the edit button, the option that is currently set will be the default choice. 我正在尝试使用JavaScript来选择文本,并且当他们按下“编辑”按钮时,当前设置的选项将是默认选项。

So for instance, if the row says the company_name is: ABC Company, when they hit the edit button, the drop down will populate with that option. 因此,例如,如果该行显示company_name为:ABC Company,则当他们单击“编辑”按钮时,下拉菜单中将填充该选项。 Since the value and text are different, I need to choose the drop down option based on text. 由于值和文本不同,因此我需要基于文本选择下拉选项。 I have tried these 2 options so far with no luck. 到目前为止,我还没有尝试过这2个选项。

To get the row text: 要获取行文本:

var d = document.getElementById("itable").rows[id].cells[3].innerText;

I have tried the following to pass back the text of the row, and to select the drop down by text. 我尝试了以下方法以传回该行的文本,并按文本选择下拉菜单。

document.querySelector('#editinsurancepolicymodal select[name=editicompanydropdown]').value = d;

This option just populates the drop down, but no choice is selected. 此选项仅填充下拉列表,但未选择任何选项。

document.querySelector('#editinsurancepolicymodal').find('option[text="InsuranceB"]').val;

This option selects the first option in the drop down, but doesn't change based on what the 'text' is. 此选项在下拉菜单中选择第一个选项,但不会根据“文本”的变化而变化。

Thank you for your help in advance. 提前谢谢你的帮助。

One way will be to iterate over the option elements, selecting the one that has the same text as the text from the row, and un-selecting all the others. 一种方法是遍历选项元素,选择与行中文本相同的文本,然后取消选择所有其他元素。 This is illustrated in the snippet below (I have inserted a 3 second timeout, so that you can see that the initially selected option (Two) is changed to the option with the text "Three" from the JavaScript code. 这在下面的代码段中进行了说明(我插入了3秒的超时,因此您可以看到JavaScript代码中最初选择的选项(Two)已更改为带有文本“ Three”的选项。

 window.setTimeout(function () { var sampleWord = "Three"; var options = document.querySelectorAll("option"); for (var i = 0; i < options.length; i++) { var option = options[i]; if (option.innerText === sampleWord) { option.selected = true; } else { option.selected = false; } } }, 3000); 
 <select id="selector"> <option id="option-1" value="1">One</option> <option id="option-2" value="2" selected>Two</option> <option id="option-3" value="3">Three</option> <option id="option-4" value="4">Four</option> </select> 

Alternately, you could store the id for the company as a data-attr attribute on the row. 或者,您可以将公司的ID作为data-attr属性存储在该行上。 For example (in PHP): 例如(在PHP中):

echo "<td data-attr-company-id=".$row['company_id'].">".$row['company_name']."</td>"

Then, you can read the attribute from the table row, and query for the option that has the same value as your attribute value. 然后,您可以从表行中读取属性,并查询与属性值具有相同值的选项。

var options = document.querySelectorAll("option");

This will select all options from the page. 这将从页面中选择所有选项。 If you want to get the options for a specific drop down, use the following: 如果要获取特定下拉菜单的选项,请使用以下命令:

var options = document.querySelector('#specificform select[name=specific_drop_down_in_the_specific_form]');

To select the option based on the text and not the value, use this loop: 要基于文本而不是值选择选项,请使用以下循环:

for (var i=0; i<options.length; i++){
        var option = options[i];
        if (option.innerText === d){
            option.selected = true;
        }
    }

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

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