如何在输入文本框中显示选定的值

我在上面的代码中使用了两个选择下拉菜单,并为两者都设置了值,并且需要显示两个地方不同的内容。请帮助我如何获得正确的结果,如果它显示为 div 或输入或按钮格式,我可以接受。


<input type="button" class="btn btn-sm btn-w1" name="txtval" id="txtval" onClick="checkVal()">

<select class="lbbg selsm-2 inblock-2 " name="rupeeitems" id="rupeeitems">

  <option value="">Select</option>

  <option value="RUB Dollar">RUB</option>

  <option value="AFN Dinaar">AFN</option>

  <option value="EUR Dollar">EUR</option>

</select>


<script>

  var select = document.getElementById('rupeeitems');

  var input = document.getElementById('txtval');

  select.onchange = function () {

    input.value = select.value;

  }

</script>

<!-- This one is working properly -->

<input type="button" class="btn btn-sm btn-w1" name="txtprice" id="txtprice" onClick="checkPrice()">

<select class="lbbg selsm-2 inblock-2 " name="cmbitems" id="cmbitems">

  <option value="">Select</option>

  <option value="USD Dollar">USD</option>

  <option value="Euro Dollar">EUR</option>

  <option value="Aud Dollar">AUD</option>

  <option value="Bahrien ">BHD</option>

</select>

<script>

  var select = document.getElementById('cmbitems');

  var input = document.getElementById('txtprice');

  select.onchange = function () {

    input.value = select.value;

  }

</script>


弑天下
浏览 132回答 3
3回答

泛舟湖上清波郎朗

您应该对变量名称更加具体。此外,您可以使用this关键字来引用函数内的当前选择元素。不确定为什么要使用内联事件处理程序:<input type="button" class="btn btn-sm btn-w1" name="txtval" id="txtval"><select class="lbbg selsm-2 inblock-2 " name="rupeeitems" id="rupeeitems">&nbsp; <option value="">Select</option>&nbsp; <option value="RUB Dollar">RUB</option>&nbsp; <option value="AFN Dinaar">AFN</option>&nbsp; <option value="EUR Dollar">EUR</option></select><!-- This one is working properly --><input type="button" class="btn btn-sm btn-w1" name="txtprice" id="txtprice"><select class="lbbg selsm-2 inblock-2 " name="cmbitems" id="cmbitems">&nbsp; <option value="">Select</option>&nbsp; <option value="USD Dollar">USD</option>&nbsp; <option value="Euro Dollar">EUR</option>&nbsp; <option value="Aud Dollar">AUD</option>&nbsp; <option value="Bahrien ">BHD</option></select><script>&nbsp; var selectRupee = document.getElementById('rupeeitems');&nbsp; var inputRupee = document.getElementById('txtval');&nbsp; selectRupee.onchange = function() {&nbsp; &nbsp; inputRupee.value = this.value;&nbsp; }&nbsp; var selectCmb = document.getElementById('cmbitems');&nbsp; var inputCmb = document.getElementById('txtprice');&nbsp; selectCmb.onchange = function() {&nbsp; &nbsp; inputCmb.value = this.value;&nbsp; }</script>

回首忆惘然

Lupu 之前在commets 中提到的,最好将所有js 代码写入<script>页面末尾标签之前的一个标签内<body>。这段代码正在运行:<input type="button" class="btn btn-sm btn-w1" name="txtval" id="txtval" onClick="checkVal()"><select class="lbbg selsm-2 inblock-2 " name="rupeeitems" id="rupeeitems">&nbsp; &nbsp; <option value="">Select</option>&nbsp; &nbsp; <option value="RUB Dollar">RUB</option>&nbsp; &nbsp; <option value="AFN Dinaar">AFN</option>&nbsp; &nbsp; <option value="EUR Dollar">EUR</option></select><!-- This one is working properly --><input type="button" class="btn btn-sm btn-w1" name="txtprice" id="txtprice" onClick="checkPrice()"><select class="lbbg selsm-2 inblock-2 " name="cmbitems" id="cmbitems">&nbsp; &nbsp; <option value="">Select</option>&nbsp; &nbsp; <option value="USD Dollar">USD</option>&nbsp; &nbsp; <option value="Euro Dollar">EUR</option>&nbsp; &nbsp; <option value="Aud Dollar">AUD</option>&nbsp; &nbsp; <option value="Bahrien ">BHD</option></select><script>&nbsp; &nbsp; var select = document.getElementById('rupeeitems');&nbsp; &nbsp; var input = document.getElementById('txtval');&nbsp; &nbsp; select.onchange = function () {&nbsp; &nbsp; &nbsp; &nbsp; input.value = select.value;&nbsp; &nbsp; }&nbsp; &nbsp; var select2 = document.getElementById('cmbitems');&nbsp; &nbsp; var input2 = document.getElementById('txtprice');&nbsp; &nbsp; select2.onchange = function () {&nbsp; &nbsp; &nbsp; &nbsp; input2.value = select2.value;&nbsp; &nbsp; }</script>您必须</select>正确关闭并且必须为变量使用不同的名称,否则您将重新分配它们的值。

慕容708150

请下次使用正确的缩进。当代码格式不正确时,代码很难阅读。这个问题很容易解决,你应该研究const、let和var以及它们各自的区别。<input type="button" class="btn btn-sm btn-w1" name="txtval" id="txtval" onClick="checkVal()">&nbsp; <select class="lbbg selsm-2 inblock-2 " name="rupeeitems" id="rupeeitems">&nbsp; &nbsp; &nbsp;<option value="">Select</option>&nbsp; &nbsp; &nbsp;<option value="RUB Dollar">RUB</option>&nbsp; &nbsp; &nbsp;<option value="AFN Dinaar">AFN</option>&nbsp; &nbsp; &nbsp;<option value="EUR Dollar">EUR</option>&nbsp; </select>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<input type="button" class="btn btn-sm btn-w1" name="txtprice" id="txtprice" onClick="checkPrice()">&nbsp; <select class="lbbg selsm-2 inblock-2 " name="cmbitems" id="cmbitems">&nbsp; &nbsp;<option value="">Select</option>&nbsp; &nbsp;<option value="USD Dollar">USD</option>&nbsp; &nbsp;<option value="Euro Dollar">EUR</option>&nbsp; &nbsp;<option value="Aud Dollar">AUD</option>&nbsp; &nbsp;<option value="Bahrien ">BHD</option>&nbsp; </select>// place your scripts here<script>&nbsp; // use descriptive variable names&nbsp; const cmbItems = document.getElementById('cmbitems');&nbsp; const txtPrice = document.getElementById('txtprice');&nbsp; const rupeeItem = document.getElementById('rupeeitems');&nbsp; const txtVal =&nbsp; document.getElementById('txtval');&nbsp; // since you are using anonimous functions&nbsp; // if you browser allows, use ES6 syntax&nbsp; cmbItems.onchange&nbsp; = () => txtPrice.value = this.value;&nbsp; rupeeItem.onchange = () => txtPrice.value = this.value;</script>&nbsp; &nbsp; &nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript