0

我对javascript很陌生。我阅读了此链接并尝试对其进行自定义,但它不起作用:Disable an input field if second input field isfilled

我想让人们在两个选项-城市和邮政编码之间切换。我想启用他们最后选择的任何字段并禁用另一个。例如,如果他们在邮政编码选项卡上并按下提交按钮,则邮政编码中的任何输入都会被提交,而不是城市,反之亦然。

html是:

<ul class="tabs">
<li><a class="border-radius top med" href="#city">City</a></li>
<li><a class="border-radius top med" href="#zipcode">Zipcode</a></li>
</ul>
<div id="city"><label class="IDX-advancedText">City</label>
<select id="aht-home-city" name="city[]" class="IDX-select " autocomplete="off">
<option value="2115">Austin</option>
<option value="2718">Bartlett</option>
</div>
<div id="zipcode"><label class="IDX-advancedText">Zipcode</label>
<input class="IDX-advancedBox IDX-advWildText" id="IDX-aw_zipcode" type="text"
   maxlength="255" name="aw_zipcode" value="" /></div>

脚本是:

var dis1 = document.getElementById("city");
dis1.onchange = function () {
if (this.value != "" || this.value.length > 0) {
  document.getElementById("zipcode").disabled = true;
  }
}
var dis2 = document.getElementById("zipcode");
dis1.onchange = function () {
if (this.value != "" || this.value.length > 0) {
  document.getElementById("city").disabled = true;
 }
}

很感谢任何形式的帮助!该网站是http://austinhometeam.staging.wpengine.com/joyce-newsletter/

4

2 回答 2

0

就像我在评论中告诉你的那样,你需要更改“getElementById”中的 ID。

当另一个为空时,我还添加了重新启用的字段。

I add an empty value in the select, when the null is selected, the zip code's field return enable.

HTML

<ul class="tabs">
<li><a class="border-radius top med" href="#city">City</a></li>
<li><a class="border-radius top med" href="#zipcode">Zipcode</a></li>
</ul>
<div id="city"><label class="IDX-advancedText">City</label>
<select id="aht-home-city" name="city[]" class="IDX-select " autocomplete="off">
<option value=""></option>
<option value="2115">Austin</option>
<option value="2718">Bartlett</option>
</select>
</div>
<div id="zipcode"><label class="IDX-advancedText">Zipcode</label>
<input class="IDX-advancedBox IDX-advWildText" id="IDX-aw_zipcode" type="text"
   maxlength="255" name="aw_zipcode" value="" /></div>

Javascript:

var dis1 = document.getElementById("aht-home-city");
var dis2 = document.getElementById("IDX-aw_zipcode");

dis1.onchange = function () {
  if (dis1.value !== "" || dis1.value.length > 0) {
    dis2.disabled = true;
  } else {
    dis2.disabled = false;
  }
};


dis2.onchange = function () {
  if (dis2.value !== "" || dis2.value.length > 0) {
    dis1.disabled = true;
  } else {
    dis1.disabled = false;
  }
};

有一个工作示例: JSBin 示例

于 2013-11-05T15:21:39.407 回答
0

寻找类似这种行为的东西?

var last = "";

var dis1 = document.getElementById("aht-home-city");
dis1.onchange = function () {
    last = "city";
    document.getElementById("IDX-aw_zipcode").disabled = true;
}
var dis2 = document.getElementById("IDX-aw_zipcode");
dis2.onchange = function () {
    last = "zipcode";
    if (this.value != "" || this.value.length > 0) {
        document.getElementById("aht-home-city").disabled = true;
    }
}

检查这个jsFiddle

注意:HTML 中还缺少一个</select>标签。

于 2013-11-05T15:21:47.220 回答