如何使用javascript选择下拉选项并更改其值

时间:2017-07-10 14:06:15

标签: javascript php jquery

我有一个下拉菜单,我需要在那里放置需要使用javascript转义的值。

我想使用javascript的原因是因为api I查询正在使用javascript并且没有收到其他方法来转义引号。

下拉值的例子:"ToxKeywords:"genotoxicity" AND ToxKeywords:"ames" OR ToxKeywords:"micronucleus""

所以我得到了这个下拉列表:

<form method="post" name="query">
    <label for="textQuery">Choose Query from list</label>
    <select class="form-control space" name="textQuery" id="textQuery">
        <option selected disabled>Choose here</option>
        <option value="ToxKeywords:">ToxKeywords</option>
        <option value="Molecules.Main_name:">Molecule</option>
        <option value="Query1">Query 1</option>
        <option value=ToxKeywords:systemic toxicity>Query 2</option>
        <option value=ToxKeywords:"phototoxicity">Query 3</option>
        <option value=ToxKeywords:"llna">Query 4</option>
    </select>

我想用javascript替换例如value="Query1"。 我试过这个:

<script>
    document.getElementById("textQuery").selectedindex = "Query 1"
    select.option.value = "ToxKeywords:\"genotoxicity\" AND ToxKeywords:\"ames\" OR ToxKeywords:\"micronucleus\"";
</script>

显然我可以选择正确的值,但我不知道如何将其更改为:"ToxKeywords:\"genotoxicity\" AND ToxKeywords:\"ames\" OR ToxKeywords:\"micronucleus\""

您能帮我找到正确的Javascript语法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用:document.querySelector(selectors);

document.querySelector('#textQuery option[value="Query1"]').value =
           'http://130.88.150.30:8983/solr/NCSTOX/select?indent=on&q=Tox‌​Keywords:%22genotoxi‌​city%22%20AND%20ToxK‌​eywords:%22ames%22%2‌​0OR%20ToxKeywords:%2‌​2micronucleus%22&wt=‌​json and i got : http://130.88.150.30:8983/solr/NCSTOX/select?indent=on&amp;q‌​=ToxKeywords:&quot;g‌​enotoxicity&quot; AND ToxKeywords:&quot;ames&quot; OR ToxKeywords:&quot;micronucleus&quot;&amp;rows=10&amp;wt=json';
            
document.getElementById('textQuery').addEventListener('change', function(e) {
  console.log(this.value);
})
<form method="post" name="query">
    <label for="textQuery">Choose Query from list</label>
    <select class="form-control space" name="textQuery" id="textQuery">
        <option selected disabled>Choose here</option>
        <option value="ToxKeywords:">ToxKeywords</option>
        <option value="Molecules.Main_name:">Molecule</option>
        <option value="Query1">Query 1</option>
        <option value=ToxKeywords:systemic toxicity>Query 2</option>
        <option value=ToxKeywords:"phototoxicity">Query 3</option>
        <option value=ToxKeywords:"llna">Query 4</option>
    </select>
</form>