输入的占位符不会从<select>更改

时间:2018-03-02 02:16:08

标签: javascript

因此,基本上我想要实现的是在更改选择时使输入的占位符发生变化。第一个输入成功更改,但这似乎与第二个输入相同。以下是我使用的代码: &#13; &#13; function onChange(fromOrTo){&#13;   if(fromOrTo =&#39; from&#39;){&#13;     document.getElementById(&#34; from&#34;)。placeholder = document.getElementById(&#34; selectFrom&#34;)。value;&#13;   } else if(fromOrTo =&#39; to&#39;){&#13;     document.getElementById(&#34; to&#34;)。placeholder = document.getElementById(&#34; selectTo&#34;)。value;&#13;   }&#13; }&#13; &LT;身体GT;&#13;   &lt; div id =&#34; sect&#34;&gt;&#13;     &lt;节&GT;&#13;       &lt; select onchange =&#34; onChange(&#39; from&#39;)&#34; ID =&#34; selectFrom&#34;&GT;&#13;         &lt; option value =&#34; Celcius&#34;&gt; Celcius&lt; / option&gt;&#13;         &lt; option value =&#34; Farenheit&#34;&gt; Farenheit&lt; / option&gt;&#13;         &lt; option value =&#34; Kelvin&#34;&gt; Kelvin&lt; / option&gt;&#13;         &lt; option value =&#34; Reamur&#34;&gt; Reamur&lt; / option&gt;&#13;       &LT; /选择&GT;&#13;       &lt; span class =&#34; toText&#34;&gt; to&lt; / span&gt;&#13;       &lt; select onchange =&#34; onChange(&#39; to&#39;)&#34; ID =&#34; selectTo&#34;&GT;&#13;         &lt; option value =&#34; Celcius&#34;&gt; Celcius&lt; / option&gt;&#13;         &lt; option value =&#34; Farenheit&#34;&gt; Farenheit&lt; / option&gt;&#13;         &lt; option value =&#34; Kelvin&#34;&gt; Kelvin&lt; / option&gt;&#13;         &lt; option value =&#34; Reamur&#34;&gt; Reamur&lt; / option&gt;&#13;       &LT; /选择&GT;&#13;     &LT; /节&gt;&#13;     &lt;节&GT;&#13;       &lt; input type =&#34; text&#34; ID =&#34;从&#34;&GT;&#13;       &lt; span id =&#34; toText&#34;&gt; to&lt; / span&gt;&#13;       &lt; input type =&#34; text&#34; ID =&#34;至&#34;&GT;&#13;     &LT; /节&gt;&#13;   &LT; / DIV&GT;&#13; &LT; /体&GT;&#13; &#13; &#13;

2 个答案:

答案 0 :(得分:1)

您只需要与===而不是=

进行比较

function onChange(fromOrTo) {
  if (fromOrTo === 'from') {
    document.getElementById("from").placeholder = document.getElementById("selectFrom").value;
  } else if (fromOrTo === 'to') {
    document.getElementById("to").placeholder = document.getElementById("selectTo").value;
  }
}
<body>
  <div id="sect">
    <section>
      <select onchange="onChange('from')" id="selectFrom">
        <option value="Celcius">Celcius</option>
        <option value="Farenheit">Farenheit</option>
        <option value="Kelvin">Kelvin</option>
        <option value="Reamur">Reamur</option>
      </select>
      <span class="toText">to</span>
      <select onchange="onChange('to')" id="selectTo">
        <option value="Celcius">Celcius</option>
        <option value="Farenheit">Farenheit</option>
        <option value="Kelvin">Kelvin</option>
        <option value="Reamur">Reamur</option>
      </select>
    </section>
    <section>
      <input type="text" id="from">
      <span id="toText">to</span>
      <input type="text" id="to">
    </section>
  </div>
</body>

答案 1 :(得分:0)

每个if条件中只有一个等号。这是变量的assigns而不是compares。切换到两个等号(或三个以避免类型强制)将解决此问题:

function onChange(fromOrTo) {
  if (fromOrTo === 'from') {
    document.getElementById("from").placeholder = document.getElementById("selectFrom").value;
  } else if (fromOrTo === 'to') {
    document.getElementById("to").placeholder = document.getElementById("selectTo").value;
  }
}
<body>
  <div id="sect">
    <section>
      <select onchange="onChange('from')" id="selectFrom">
        <option value="Celcius">Celcius</option>
        <option value="Farenheit">Farenheit</option>
        <option value="Kelvin">Kelvin</option>
        <option value="Reamur">Reamur</option>
      </select>
      <span class="toText">to</span>
      <select onchange="onChange('to')" id="selectTo">
        <option value="Celcius">Celcius</option>
        <option value="Farenheit">Farenheit</option>
        <option value="Kelvin">Kelvin</option>
        <option value="Reamur">Reamur</option>
      </select>
    </section>
    <section>
      <input type="text" id="from">
      <span id="toText">to</span>
      <input type="text" id="to">
    </section>
  </div>
</body>