Javascript-форум (https://javascript.ru/forum/)
-   Элементы интерфейса (https://javascript.ru/forum/dom-window/)
-   -   Как подружить два select (https://javascript.ru/forum/dom-window/81668-kak-podruzhit-dva-select.html)

Morris 10.01.2021 11:53

Как подружить два select
 
Есть два селекта, по одиночке коды работают, а вмести нет. с r1 что то не коректно.
<!DOCTYPE html>
<html>
<head>
    <title>Селекты</title>
    <meta charset="utf-8">
</head>
<script type="text/javascript">
function calculat (obj) {

 var select = document.querySelector('r1');
		var val = [{a : 10},{a : 2},{a : 1}][select.value];
		var q = val.a;
 obj.res1.value= q;
 
 var m = document.getElementById("r2");
var s = m.value;
 obj.res2.value= s;
}
</script>
<form >
<select id="r1" >
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<input type="text" style="width:55px" id="res1"><br>

<select  id="r2">
<option value="14">4</option>
<option value="15">5</option>
<option value="16">6</option>
</select>
<input type="text" style="width:55px" id="res2">

<input type="button" name="WB" value="Ok" onclick="calculat(form)">
</form>
</html>

рони 10.01.2021 14:08

Morris,
строка 10, решётка по вам плачет :)
Основные виды селекторов

Morris 10.01.2021 14:43

Вот так заработало)
<!DOCTYPE html>
<html>
<head>
    <title>Селекты</title>
    <meta charset="utf-8">
</head>
<script type="text/javascript">
function calculat (obj) {

 var select = document.querySelector('#r1');
		var val = [{a : 10},{a : 2},{a : 1}][select.value];
		var q = val.a;
 obj.res1.value= q;
 
 var m = document.getElementById("r2");
var s = m.value;
 obj.res2.value= s;
}
</script>
<form >
<select id="r1" >
<option value="0"selected>1</option>
<option value="1">2</option>
<option value="2">3</option>
</select>
<input type="text" style="width:55px" id="res1"><br>

<select  id="r2">
<option value="14">4</option>
<option value="15">5</option>
<option value="16">6</option>
</select>
<input type="text" style="width:55px" id="res2">

<input type="button" name="WB" value="Ok" onclick="calculat(form)">
</form>
</html>

рони 10.01.2021 15:00

Morris,
параметр run добавьте в тег форматирования
например:
[html run]
... минимальный код страницы с вашей проблемой
[/html]

О том, как вставить в сообщение исполняемый javascript и html-код, а также о дополнительных возможностях форматирования - читайте http://javascript.ru/formatting.

Morris 10.01.2021 17:06

вот такая была задумка. Может чего и намудрил, но теперь работает)
<!DOCTYPE html>
<html>
<head>
    <title>Селекты</title>
    <meta charset="utf-8">
</head>
<script type="text/javascript">
function calculat (obj) {

        var num55= obj.Number55.value.replace(/,/,".");
        var num56= obj.Number56.value.replace(/,/,".");
	var num57= obj.Number57.value.replace(/,/,".");
        var num58= obj.Number58.value.replace(/,/,".");
 var select = document.querySelector('#qr2');
		var val = [{a : num55},{a :num56 },{a : num57},{a : num58}][select.value];
		var qr2 = val.a;
 obj.res1578.value= qr2;
 
 var msel = document.getElementById("r1");
var q = msel.value;
 obj.res2.value= q;
}
</script>
<form >
<select id="qr2" >
<option value="0"selected>10</option>
<option value="1">12</option>
<option value="2">14</option>
<option value="3">16</option>
</select>
<input type="text" style="width:55px" id="res1578"><br><br>
10 <input type="text" value="1.9" style="width:26px"  name="Number55">&nbsp;&nbsp; 12 <input type="text" value="1.4" style="width:26px"  name="Number56">
&nbsp;&nbsp; 14 <input type="text" value="1.56" style="width:26px"  name="Number57">&nbsp;&nbsp; 16 <input type="text" value="0.98" style="width:26px"  name="Number58">
<br><br>
<select  id="r1">
<option value="10">0.1</option>
<option value="2">0.5</option>
<option value="1">1</option>
</select>
<input type="text" style="width:55px" id="res2">
<br><br>
<input type="button" name="WB" value="Ok" onclick="calculat(form)">
</form>
</html>

рони 10.01.2021 17:32

Morris,
<!DOCTYPE html>
<html>

<head>
    <title>Селекты</title>
    <meta charset="utf-8">
    <style type="text/css">
    label {
        display: table-row;
    }
    </style>
</head>
<script type="text/javascript">
    function calculat(obj) {
        var num = document.querySelectorAll('[name^="Number"]')[qr2.value].value;
        obj.res1578.value = num.replace(/,/, ".");
        obj.res2.value = obj.r1.value;
    }
</script>
<form>
<select id="qr2">
<option value="0"selected>10</option>
<option value="1">12</option>
<option value="2">14</option>
<option value="3">16</option>
</select>
<input type="text" style="width:55px" id="res1578">

<label>10<input type="text" value="1.9" style="width:26px" name="Number55"></label>
<label>12<input type="text" value="1.4" style="width:26px" name="Number56"></label>
<label>14<input type="text" value="1.56" style="width:26px" name="Number57"></label>
<label>16<input type="text" value="0.98" style="width:26px" name="Number58"></label>

<select id="r1">
<option value="10">0.1</option>
<option value="2">0.5</option>
<option value="1">1</option>
</select>
<input type="text" style="width:55px" id="res2">
    <br><br>
<input type="button" name="WB" value="Ok" onclick="calculat(form)">
</form>

</html>

Morris 10.01.2021 19:00



Часовой пояс GMT +3, время: 07:48.