0

クリックしたときに別の選択ボックスの値を使用して、ある選択ボックスにデータを入力する関数を作成しました。これは、システム内のほとんどの選択ボックスでうまく機能しますが、現在、これに苦労しています笑:

選択した値をテストし、見つかった場合は警告する関数:

function updateSelectBox(parent, child){
    for(i = 0; i < document.getElementById(parent).length; i++){
        if(document.getElementById(parent).options[i].selected){
            alert(document.getElementById(parent).options[i].value);
    }
}

これは、選択されたときに警告を発しません:

<input type="hidden" name="data[Series][3][Profession][Profession]" value="" id="Professions3_">
<select name="data[Series][3][Profession][Profession][]" multiple="multiple" id="Professions3" onchange="updateSelectBox("Professions3", "Specialisms3");">
<option value="24">Scientist</option>

これは、選択すると警告します。

<input type="hidden" name="data[Series][4][Profession][Profession]" value="" id="Professions4_">
<select name="data[Series][4][Profession][Profession][]" multiple="multiple" id="Professions4" onchange="updateSelectBox("Professions4", "Specialisms4");">
<option value="24">Scientist</option>

これは、動作している別の選択ボックスからの完全な html 出力です

<div class="input select"><label for="Zones">Zones</label><input type="hidden" name="data[Series][0][Zone][Zone]" value="" id="Zones_">
<select name="data[Series][0][Zone][Zone][]" multiple="multiple" id="Zones" onchange="updateSelectBox(&quot;Zones&quot;, &quot;Countries&quot;);">
<option value="4">Europe</option>
</select>
</div>
4

1 に答える 1

0

これは機能しません。ページで機能する場合でも、投稿のコードは機能しません。JavaScriptに閉じ括弧がない、二重引用符で囲まれたHTLMを使用しているため、誰もこれを実行できません。それでは、一歩下がって、これを間違いなく機能するものに書き直してみましょう。

HTML

<select onchange="updateSelectBox(this);">
  <option value="0">Scientist 1</option>
  <option value="12">Scientist 2</option>
  <option value="24">Scientist 3</option>
</select>

JS

function updateSelectBox(selectBox) {
  var sid = selectBox.selectedIndex;
  var selectedOption = selectBox.children[sid];
  console.log(selectedOption);
}

これで、少なくとも、最小限のコードを使用して、元に戻すことができることがわかっているものができました。これを使用して、現在使用しているHTMLと関数を構築することをお勧めします。たとえば、非常に多くの文字列やルックアップの使用を確実にやめる必要があります。特に文字列にタイプミスがないことを信頼すると、1日が台無しになります(「Professions3_」という入力と「Professions3」という選択があります...これは問題を引き起こしているだけです)

于 2013-02-06T17:25:11.047 に答える