1

同様の質問が投稿されているのを見て、ニーズに合わせて変更しようとしましたが、それを行うための JavaScript について十分に知りません。ユーザーがドロップダウンの選択を変更すると、「タイトルテキストフィールド」の最大長が動的に変更される必要があります

a、bc、および d の最大 maxlength は 40 で、maxlength は 2 である必要があります

私のコードは以下のとおりです。理由はわかりませんが、正しく動作していません。選択オプションを「E」に変更すると、力価入力ボックスの maxLength が 2 ではなくデフォルト値 (40) になります。これが問題です。2 である必要があります。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>title page</title>
    <script type="text/javascript">
      function changeValue(dropdown) {
        var option = dropdown.options[dropdown.selectedIndex].value,
            field = document.getElementById('titre');

        if (option == 'a' || option == 'b' || option == 'c' || option == 'd') {
          field.maxLength = 40;
        } else if (option == 'e') {
          field.value = field.value.substr(0, 2); // before reducing the maxlength, make sure it contains at most two characters; you could also reset the value altogether
          field.maxLength = 2;
        }
      }?
    </script>
  </head>
  <body>
    <form action="converter.php" method="post">
      <h2>Feel all field below:</h2>
      <div>
        Title: <input type="texte" name="titre" id="titre" maxLength="40"/>  Format: 
        <select id="format" name="format" onchange="changeValue(this);">
          <option value="a">A</option>
          <option value="b">B</option>
          <option value="c">C</option>
          <option value="d">D</option>
          <option value="e">E</option>
        </select>
      </div>
      <div>
        <textarea name="texte" style="width: 415px; height: 155px;"></textarea>
      </div>
      <div>
        <input type="submit" value="OK" />
      </div>
    </form>
  </body>
</html>

4

1 に答える 1

1

関数定義の最後に疑問符があります。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>title page</title>
        <script type="text/javascript">
            function changeValue(dropdown) {
                var option = dropdown.options[dropdown.selectedIndex].value,
                field = document.getElementById('titre');

                if (option == 'e') {
                    field.value = field.value.substr(0, 2); // before reducing the maxlength, make sure it contains at most two characters; you could also reset the value altogether
                    field.maxLength = 2;
                } else {
                    field.maxLength = 40;
                }
            }
        </script>
    </head>
    <body>
        <form action="converter.php" method="post">
            <h2>Feel all field below:</h2>
            <div>
                Title: <input type="texte" name="titre" id="titre" maxLength="40"/>  Format: 
                <select id="format" name="format" onchange="changeValue(this);">
                    <option value="a">A</option>
                    <option value="b">B</option>
                    <option value="c">C</option>
                    <option value="d">D</option>
                    <option value="e">E</option>
                </select>
                </div>
                <div>
                    <textarea name="texte" style="width: 415px; height: 155px;"></textarea>
                </div>
                <div>
                <input type="submit" value="OK" />
            </div>
        </form>
    </body>
</html>
于 2012-12-19T17:13:57.480 に答える