0

IDと値に応じてその場でフォーム要素を作成するCMS Webサイトに取り組んでいます。リンクを使用して、これらのフォームから特定の情報を選択する必要があります。

ラジオ オプションとチェックボックス オプションについては、次のコードを使用します <a href="#" onclick="document.getElementById('RAL1').checked=true">1001</a> 。RAL1はチェックしたいラジオのIDです。選択では、ID の後にオプションを指定する必要があります。ID から値を選択する必要があるため、ここで問題が発生します。

フォームが作成するコードは

<select id="Zinc_plated_field" class="inputboxattrib" name="Zinc_plated12">
  <option value="no">no</option>
  <option value="yes">yes (+€871.08)</option>
</select>

私はほぼすべてを試しましたが、運がありません。誰かが私を正しい方向に向けることができますか?

ありがとう!

4

2 に答える 2

0

Just about every browser supports the following statement to get the selected value ("no" or "yes"):

document.getElementById('Zinc_plated_field').value

For very old browsers you would have to use:

var sel = document.getElementById('Zinc_plated_field');
sel.options[sel.selectedIndex].value;

If you want to set the selected value, there are several ways to do it:

document.getElementById('Zinc_plated_field').value = 'yes';

or

document.getElementById('Zinc_plated_field').options[1].selected = true;

or

document.getElementById('Zinc_plated_field').selectedIndex = 1;

The two latter use the index of the option you want to set (one specifies which index is selected by setting the selectedIndex property of the <select> element, while the other sets the selected attribute of the <option> element to true.)

I can't say which is most cross-browser compatible off-hand, but if the first one doesn't work, just try the other two.

于 2009-11-02T08:23:16.980 に答える
0

これはFirefoxとSafariで機能します:

<a href="#" onclick="document.getElementById('Zinc_plated_field').value = 'yes'">yes</a>
<a href="#" onclick="document.getElementById('Zinc_plated_field').value = 'no'">no</a>
于 2009-11-02T08:58:15.430 に答える