3 回答

TA貢獻1818條經驗 獲得超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">
<option value="">Select</option>
<option value="RUB Dollar">RUB</option>
<option value="AFN Dinaar">AFN</option>
<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">
<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 selectRupee = document.getElementById('rupeeitems');
var inputRupee = document.getElementById('txtval');
selectRupee.onchange = function() {
inputRupee.value = this.value;
}
var selectCmb = document.getElementById('cmbitems');
var inputCmb = document.getElementById('txtprice');
selectCmb.onchange = function() {
inputCmb.value = this.value;
}
</script>

TA貢獻1847條經驗 獲得超11個贊
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">
<option value="">Select</option>
<option value="RUB Dollar">RUB</option>
<option value="AFN Dinaar">AFN</option>
<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">
<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('rupeeitems');
var input = document.getElementById('txtval');
select.onchange = function () {
input.value = select.value;
}
var select2 = document.getElementById('cmbitems');
var input2 = document.getElementById('txtprice');
select2.onchange = function () {
input2.value = select2.value;
}
</script>
您必須</select>正確關閉并且必須為變量使用不同的名稱,否則您將重新分配它們的值。

TA貢獻1831條經驗 獲得超4個贊
請下次使用正確的縮進。當代碼格式不正確時,代碼很難閱讀。
這個問題很容易解決,你應該研究const、let和var以及它們各自的區別。
<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>
<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>
// place your scripts here
<script>
// use descriptive variable names
const cmbItems = document.getElementById('cmbitems');
const txtPrice = document.getElementById('txtprice');
const rupeeItem = document.getElementById('rupeeitems');
const txtVal = document.getElementById('txtval');
// since you are using anonimous functions
// if you browser allows, use ES6 syntax
cmbItems.onchange = () => txtPrice.value = this.value;
rupeeItem.onchange = () => txtPrice.value = this.value;
</script>
添加回答
舉報