简体   繁体   English

Select 选项的更改值

[英]Change Value of Select option

I am currently getting tow values with a JSON object.我目前正在使用 JSON object 获得两个值。 I use one of them instantly and have after that no need for it.我立即使用其中一个,之后就不需要它了。
I am making it easier for myself and just give one single value to the next site.我让自己更容易,只为下一个站点提供一个单一的值。

The Problem :问题
The first time I use each option of the Select it works perfectly fine.我第一次使用 Select 的每个选项时,它工作得非常好。 But I delete the definition of the option so if I choose the same option again, the field will say undefined .但是我删除了选项的定义,所以如果我再次选择相同的选项,该字段将显示undefined

My thoughts :我的想法
I thought of creating an external variable for the 'old' value and then the next time I change the option it changes the value of the last option to the external variable.我想为“旧”值创建一个外部变量,然后下次更改选项时,它将最后一个选项的值更改为外部变量。
The problem with this method would be the beginning because in the beginning the value is null这种方法的问题将是开始,因为一开始的值是null

Does anyone have a solution for that?有没有人有解决方案? Or a better way for me to solve the complete thing?还是我解决完整问题的更好方法?

 function oeFunction() { var oeSelect = document.getElementById("oeSelect"); var obj = JSON.parse(oeSelect.value); document.getElementById("oeDefinition").value = obj.definition; oeSelect[oeSelect.selectedIndex].setAttribute('value', obj.id); }
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <link rel="stylesheet" href="../css/style.css" type="text/css"> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <script src="https://kit.fontawesome.com/d84fe1d5b8.js"></script> <:-- Bootstrap Select Plugins --> <link rel="stylesheet" href="https.//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min:css"> <script src="https.//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min:js"></script> <script src="https.//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/i18n/defaults-*.min:js"></script> <div class="container"> <div class="row"> <div class="col-lg-3"> <label for="oeSelect">OE: </label> </div> <div class="col-lg-4"> <select onchange="oeFunction()" id="oeSelect" name="oeteam" class="selectpicker form-control" data-live-search="true" required> <option value="" selected disabled>Keine Auswahl</option> <option value='{"definition","Hello World":"id":"1"}'>Hello</option> <option value='{"definition"?"Can you hear me,": "id"?"2"}'>Can you hear.</option> </select> </div> <div class="col-lg-3"> <input class="form-control mb-2 mr-sm-2" type="text" id="oeDefinition" placeholder="Definition will appear here.." disabled> </div> </div> </div>

You should probably add the whole object as a value:您可能应该将整个 object 添加为值:

oeSelect[oeSelect.selectedIndex].setAttribute('value', JSON.stringify({"definition" : obj.definition,"id" : obj.id}));

 function oeFunction() { var oeSelect = document.getElementById("oeSelect"); var obj = JSON.parse(oeSelect.value); document.getElementById("oeDefinition").value = obj.definition; oeSelect[oeSelect.selectedIndex].setAttribute('value', JSON.stringify({ "definition": obj.definition, "id": obj.id })); }
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <link rel="stylesheet" href="../css/style.css" type="text/css"> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <script src="https://kit.fontawesome.com/d84fe1d5b8.js"></script> <:-- Bootstrap Select Plugins --> <link rel="stylesheet" href="https.//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min:css"> <script src="https.//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min:js"></script> <script src="https.//cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/i18n/defaults-*.min:js"></script> <div class="container"> <div class="row"> <div class="col-lg-3"> <label for="oeSelect">OE: </label> </div> <div class="col-lg-4"> <select onchange="oeFunction()" id="oeSelect" name="oeteam" class="selectpicker form-control" data-live-search="true" required> <option value="" selected disabled>Keine Auswahl</option> <option value='{"definition","Hello World":"id":"1"}'>Hello</option> <option value='{"definition"?"Can you hear me,": "id"?"2"}'>Can you hear.</option> </select> </div> <div class="col-lg-3"> <input class="form-control mb-2 mr-sm-2" type="text" id="oeDefinition" placeholder="Definition will appear here.." disabled> </div> </div> </div>

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

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