1

最初のオプションが空白 (何も選択されていない) の 2 つの相互排他的 OR cfselect があります。cfselect で空白以外のオプションを選択すると、別の cfselect の空白のオプション (最初のオプション) が javascript 関数で選択されます。ただし、オプションをクリックしても JavaScript 関数は呼び出されません (alert() でテスト済み)。

function resetBusiness() {
  alert("resetBusiness() - Entering ..................."");

  var aForm = document.forms[0];
  var optDiv = aForm.getElementById("sDiv").value;

  alert("optDiv: |" + optDiv + "|");

  if (optDiv != "") {
    aForm.getElementById('sBus').selectedIndex  = 0;
    aForm.submit();
  }
}


function resetDivision() {
  alert("resetDivision() - Entering ..................."");

  var aForm = document.forms[0];
  var optBus  = aForm.getElementById("sBus").value;

  alert("optBus: |" + optBus + "|");

  if (optBus != "") {
    aForm.getElementById('sDiv').selectedIndex  = 0;
    aForm.submit();
  }
}

...

<cfselect id="sBus" name="sBus" query="qryBus" display="BU" value="BU"
          selected="#HDF(attributes.sBus)#" multiple="yes" size="6"
          queryPosition="below"
          onClick="javascript:resetDivision(this)">
  <option></option>
</cfselect>
4

1 に答える 1

0

@jbabey は正しいです。アラートに引用符が多すぎます。これに変更してみてください:

alert("Entering resetDivision function...");

document.forms[0]また、コードを変更してこのフォームの上に別のフォームを追加する場合 (ページ上部の検索ボックスなど) に備えて、id を使用する代わりに、フォームにも id を付与することをお勧めします。私は見たことがないform.getElementById()...

function resetDivision() {
  var optBus = document.getElementById("sBus").value;

  if (optBus != "") {
    document.getElementById('sDiv').selectedIndex = 0;
    document.getElementById("form1").submit();  // substitute with actual form id
  }
}

コードは、要素が存在することも前提としています。コードをより堅牢にして、正常にフェッチされた場合にのみオブジェクトのプロパティにアクセスしようとすることができます。

var optBus = document.getElementById("sBus");
var optBusVal = '';
if (optBus && optBus != 'undefined') {  // actually I think you only need the first check
    optBusVal = optBus.value;
}
于 2013-05-31T23:55:38.270 に答える