根据选择的选项更改输入值

时间:2017-06-22 14:22:19

标签: javascript

我想根据选择的选项更改input field的值。 我做了这个功能,但它没有给我任何结果:

<script>

   function changeFunc() {
    var idpn = document.getElementById("idpn").value;       

    if(idpn.startsWith('v531')) {
       if(document.getElementById("price_type_chosen").options.selectedIndex = 2) {

            var ltvalue =document.getElementById("ltid").value=30;
        } else if (document.getElementById("price_type_chosen").options.selectedIndex = 3) {

            var ltvalue =document.getElementById("ltid").value=30;
         }

    } 
        alert('ok');
}

  </script>

请告诉我函数中的错误在哪里。

3 个答案:

答案 0 :(得分:1)

你没有比较,你正在分配价值

if(document.getElementById("price_type_chosen").options.selectedIndex = 2) 

答案 1 :(得分:1)

您需要在比较中使用from bs4 import BeautifulSoup soup = BeautifulSoup(html_doc, 'html.parser') soup.find_all("a", href=re.compile("YOUR REGEX")

==

答案 2 :(得分:0)

= 语句中更改 2错误比较(==if)后,代码正常运行。< / p>

我还合并了if/else语句,并将document.getElementById("price_type_chosen").options.selectedIndex的值放入变量中以使其更具可读性。

&#13;
&#13;
function changeFunc() {
  var idpn = document.getElementById("idpn").value;

  if (idpn.startsWith('v531')) {
    var selectedIndex = document.getElementById("price_type_chosen").options.selectedIndex;

    //both values are in 1 'if statement' with an or operator
    if (selectedIndex == 2 || selectedIndex == 3) {
      var ltvalue = document.getElementById("ltid").value = 30;
    }
  }
  alert('ok');
}
&#13;
price_type_chosen:
<select id="price_type_chosen">
  <option>0</option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
</select>
<br>

<label>idpn:</label><input id="idpn" type="text" value="v531" />
<br>
<label>ltid:</label><input id="ltid" type="text" value="" />
<br><br>
<button onclick="changeFunc()">changeFunc</button>
&#13;
&#13;
&#13;

选择选项2或3将值30放在#ltid

相关问题