将单选按钮链接到下拉菜单

时间:2018-06-26 18:56:09

标签: javascript html web-frontend

我希望在不使用任何if / else / while / for /任何控制流语句的情况下完成此小项目,而只是使用纯JavaScript,但是我似乎遇到了问题。我只是想将单选按钮连接到下拉菜单,以便在单击一个时突出显示另一个。例如,现在我有了它,以便当您使用setRB()函数从下拉菜单中选择一个项目时,它会将相应的单选按钮设置为true。

我现在想使单选按钮将true设置为相应的下拉菜单项。可以使用上述条件完成此操作,还是需要使用控制流语句(我相信这就是它们的全部称呼)。

谢谢。

function setRb() {
  //Point to options list
  //var e = document.getElementById('selColors');
  var e = document.jon.resistors;
  //Get value of color selected
  var s = e.options[e.selectedIndex].value;
  //Now get radio button obect and check it
  var c = document.getElementById(s);
  c.checked = true;
}
function setList(radio) {
  alert(radio); 
}

<form name=jon>
    <select id="selColors" name="resistors">
      <option value="rbBlack">Black</option>
      <option value="rbBlue">Blue</option>
      <option value="rbBrown">Brown</option>
      <option value="rbGrey">Grey</option>
      <option value="rbGreen">Green</option>
      <option value="rbOrange">Orange</option>
      <option value="rbRed">Red</option>
      <option value="rbViolet">Violet</option>
      <option value="rbYellow">Yellow</option>
      <option value="rbWhite">White</option>
    </select>
    <input onclick="setRb()" type="button" value="Submit">


<div>
    <table>
    <tr>
    <td><input onclick="setList(this.value)" id="rbBlack" value="Black" type="radio" name="colors" />Black</td>
    <td><input onclick="setList(this.value)" id="rbBlue" value="Blue" type="radio" name="colors" />Blue</td>
    <td><input onclick="setList(this.value)" id="rbBrown" value="Brown" type="radio" name="colors" />Brown</td>
    <td><input onclick="setList(this.value)" id="rbGrey" value="Grey" type="radio" name="colors" />Grey</td>
    <td><input onclick="setList(this.value)" id="rbGreen" value="Green" type="radio" name="colors" />Green</td>
    <td><input onclick="setList(this.value)" id="rbOrange" value="Orange" type="radio" name="colors" />Orange</td>
    <td><input onclick="setList(this.value)" id="rbRed" value="Red" type="radio" name="colors" />Red</td>
    <td><input onclick="setList(this.value)" id="rbViolet" value="Violet" type="radio" name="colors" />Violet</td>
    <td><input onclick="setList(this.value)" id="rbYellow" value="Yellow" type="radio" name="colors" />Yellow</td>
    <td><input onclick="setList(this.value)" id="rbWhite"value="White" type="radio" name="colors" />White</td>
    </tr>
    </table>
</div>

2 个答案:

答案 0 :(得分:2)

是的,有可能。

具有选定选项值的访问选项:

document.querySelector("option[value='x']")

function setRb() {
  //Point to options list
  //var e = document.getElementById('selColors');
  var e = document.jon.resistors;
  //Get value of color selected
  var s = e.options[e.selectedIndex].value;
  //Now get radio button obect and check it
  var c = document.getElementById(s);
  c.checked = true;
}

// Set current option null
var currOpt = null;

function setList(e) {
  // Value is the id of the clicked radio button
  var val = e.id;
  // Access current option
  currOpt = document.querySelector("option[value=" + val + "]");
  // Set current option "selected" 
  currOpt.setAttribute("selected", "");
}
<form name=jon>
  <select id="selColors" name="resistors">
    <option value="rbBlack">Black</option>
    <option value="rbBlue">Blue</option>
    <option value="rbBrown">Brown</option>
    <option value="rbGrey">Grey</option>
    <option value="rbGreen">Green</option>
    <option value="rbOrange">Orange</option>
    <option value="rbRed">Red</option>
    <option value="rbViolet">Violet</option>
    <option value="rbYellow">Yellow</option>
    <option value="rbWhite">White</option>
  </select>
  <input onclick="setRb()" type="button" value="Submit">
</form>

<div>
  <table>
    <tr>
      <td>
        <input onclick="setList(this)" id="rbBlack" value="Black" type="radio" name="colors" />Black</td>
      <td>
        <input onclick="setList(this)" id="rbBlue" value="Blue" type="radio" name="colors" />Blue</td>
      <td>
        <input onclick="setList(this)" id="rbBrown" value="Brown" type="radio" name="colors" />Brown</td>
      <td>
        <input onclick="setList(this)" id="rbGrey" value="Grey" type="radio" name="colors" />Grey</td>
      <td>
        <input onclick="setList(this)" id="rbGreen" value="Green" type="radio" name="colors" />Green</td>
      <td>
        <input onclick="setList(this)" id="rbOrange" value="Orange" type="radio" name="colors" />Orange</td>
      <td>
        <input onclick="setList(this)" id="rbRed" value="Red" type="radio" name="colors" />Red</td>
      <td>
        <input onclick="setList(this)" id="rbViolet" value="Violet" type="radio" name="colors" />Violet</td>
      <td>
        <input onclick="setList(this)" id="rbYellow" value="Yellow" type="radio" name="colors" />Yellow</td>
      <td>
        <input onclick="setList(this)" id="rbWhite" value="White" type="radio" name="colors" />White</td>
    </tr>
  </table>
</div>

答案 1 :(得分:1)

要将“选择”连接到“单选”按钮-,反之亦然 ,您可以这样做:

const EL = sel => document.querySelectorAll(sel);
const connect = ev => {
  [...EL(`[value="${ev.target.value}"]`)].forEach(el => 
    el.type === "radio" ? el.checked = true : el.selected = true
  )
};

// Connect select to radio buttons and vice-versa
[...EL('[name=resistors], [name=colors]')].forEach(el => el.addEventListener('change', connect));
<select name="resistors">
  <option value="Black">Black</option>
  <option value="Blue">Blue</option>
  <option value="Brown">Brown</option>
  <option value="Grey">Grey</option>
  <option value="Green">Green</option>
  <option value="Orange">Orange</option>
  <option value="Red">Red</option>
  <option value="Violet">Violet</option>
  <option value="Yellow">Yellow</option>
  <option value="White">White</option>
</select>

<br>

<label><input type="radio" name="colors" value="Black">Black</label>
<label><input type="radio" name="colors" value="Blue">Blue</label>
<label><input type="radio" name="colors" value="Brown">Brown</label>
<label><input type="radio" name="colors" value="Grey">Grey</label>
<label><input type="radio" name="colors" value="Green">Green</label>
<label><input type="radio" name="colors" value="Orange">Orange</label>
<label><input type="radio" name="colors" value="Red">Red</label>
<label><input type="radio" name="colors" value="Violet">Violet</label>
<label><input type="radio" name="colors" value="Yellow">Yellow</label>
<label><input type="radio" name="colors" value="White">White</label>

以上使用ES6语法,Sun请提供您可以在this jsFiddle link中找到的ES5版本