如果2个下拉选项为true,则设置文本框值?

时间:2017-12-12 14:09:38

标签: javascript

有人可以告诉我为什么这段代码不起作用?如果2个下拉选项为真,我正在尝试将文本放入文本框中。

function test() {
  if (document.getElementById("Accounts").value == "Account1" && document.getElementById("DeliveryPlaces").value == "1") {
    document.getElementById("Organisation1").value = "Company1";
  }
}
<select id="Accounts">
    <option value="Account1">Account1</option>
    <option value="Account2">Account2</option>
    <option value="Account3">Account3</option>
    <option value="Account4">Account4</option>
    </select>

<select id="DevliveryPlaces">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    </select>

<input type="text" id="Organisation1" />

“#Accounts”以及“#DeliveryPlaces”是下拉列表,“#Organisation1”是文本框。

5 个答案:

答案 0 :(得分:1)

您可以通过向每个select元素添加hash.transform_keys!(&:to_sym) 来完成此操作。每次onChange运行时,它都会将值重置为空,然后如果onChange语句应用,则更改值:

if
function test() {

document.getElementById("Organisation1").value = "";

if (document.getElementById("Accounts").value == "Account1" && 
document.getElementById("DeliveryPlaces").value == "1") {
   document.getElementById("Organisation1").value = "Company1";
}
}

答案 1 :(得分:1)

所有帐户与所有地点进行比较:

accountsValue.replace("Account", "") === placesValue

如果您想使用JavaScript更改HTML内容,则必须添加 事件

el.addEventListener("event", function() {})

总计(HTML修改)

&#13;
&#13;
var accounts = document.getElementById("accounts"),
  places = document.getElementById("places"),
  organisation = document.getElementById("organisation");

function test() {
  var accountsValue = accounts.value,
    placesValue = places.value;
  if (accountsValue.replace("Account", "") === placesValue) {
    organisation.value = "Organisation" + placesValue;
  } else {
    organisation.value = "";
  }
}

test();

accounts.addEventListener("change", test);
places.addEventListener("change", test);
&#13;
<select id="accounts">
  <option value="Account1">Account1</option>
  <option value="Account2">Account2</option>
  <option value="Account3">Account3</option>
  <option value="Account4">Account4</option>
</select>

<select id="places">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select>

<input type="text" id="organisation" />
&#13;
&#13;
&#13;

<强>提示

将JavaScript放在<body>

的末尾

答案 2 :(得分:0)

无论您遇到什么问题,它都不在您的代码中,因为它正在运行:

function test() {
  if (document.getElementById("Accounts").value == "Account1" &&
    document.getElementById("DeliveryPlaces").value == "1") {
    document.getElementById("Organisation1").value = "Company1";
  } else {
    document.getElementById("Organisation1").value = "Not Company 1";
  }
}

document.querySelector("button").addEventListener("click", test);
<select id="DeliveryPlaces">
<option value="1">DeliveryPlace 1</option>
<option value="2">DeliveryPlace 2</option>
</select>

<select id="Accounts">
<option value="Account1">Account 1</option>
<option value="Account2">Account 2</option>
</select>

<textarea id="Organisation1"></textarea>

<button>test</button>

答案 3 :(得分:0)

试试这个,您应该获得所选选项的值并进行比较

  function test() {
        var ddlAccounts = document.getElementById("Accounts");
        var AccountsValue = ddlAccounts.options[e.selectedIndex].value;
        var ddlDeliveryPlaces = document.getElementById("DeliveryPlaces");
        var DeliveryPlacesValue = ddlDeliveryPlaces.options[e.selectedIndex].value;

        if (AccountsValue == "Account1" && DeliveryPlacesValue == "1") {
            document.getElementById("Organisation1").value = "Company1";
        }
    }

答案 4 :(得分:0)

有拼写错误选择ID =&#34; DevliveryPlaces&#34; ,您需要调用该函数。您可以通过添加指向SWC的事件侦听器来执行此操作。

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <select id="Accounts">
    <option value= "select"></option>
    <option value="Account1">Account1</option>
    <option value="Account2">Account2</option>
    <option value="Account3">Account3</option>
    <option value="Account4">Account4</option>
  </select>

  <select id="DeliveryPlaces">
    <option value= "select"></option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
  </select>

  <input type="text" id="Organisation1" />


  <script type="text/javascript">
    function test() {
      // alert(document.getElementById("DeliveryPlaces").value);
      if (document.getElementById("Accounts").value == "Account1" && document.getElementById("DeliveryPlaces").value == "1") {
        // alert("Working");
        document.getElementById("Organisation1").value = "Company1";
      }
    }
    var deliveryPlaces = document.getElementById('DeliveryPlaces');
    var accounts = document.getElementById('Accounts');
    deliveryPlaces.addEventListener('change', test);
    accounts.addEventListener('change', test);

  </script>
</body>
</html>
&#13;
&#13;
&#13;