6

登録ページのフォームがあります。フォームには、状況に応じて src を変更する画像が含まれています。フォームの送信時にアクティブになるスクリプト内で、その画像に特定の src がある場合にフォームがアラートを呼び出すようにしたいので、値を取得して比較する方法が必要です。

HTML:

<form action="register_submit.php" method="post" name="mainform" enctype="multipart/form-data" onSubmit="return checkForm(this);return false;">

JS:

    function checkForm(f)
    {if ([image src value] == "pictures/apic.png")
       {
          alert("error picture is apic");
          return false;
       }
     else
       {
          f.submit();
          return false;
       }
    }

完全な相対コードは次のとおりです。

    <script type="text/javascript">
            function checkForm(f)
            {if ([image src value] == "pictures/apic.png")
               {
                  alert("error picture is apic");
                  return false;
               }
             else
               {
                  f.submit();
                  return false;
               }
            }
    </script>

    <form action="register_submit.php" method="post" name="mainform" enctype="multipart/form-data" onSubmit="return checkForm(this);return false;">

    <div class="required">
    <label for="first_name">*First Name:</label>
    <input type="text" name="first_name" id="first_name" class="inputText" onkeyup="checkFName(this.value);" onblur="checkFName(this.value);" maxlength="20" size="10" value="" />
    <img id="FName_Status" name="FName_Status" src="/pictures/bad.png" style="margin-left:5px; position:absolute;" alt="FName_Status" />
    </div>

    (OTHER OBJECTS)
    </form>

<input type="submit"  name="sub" class="inputSubmit" value="Submit &raquo;"/>
4

2 に答える 2

17

これを使って:

if (document.getElementById('FName_Status').getAttribute('src') == "pictures/apic.png")
于 2012-06-01T01:29:42.307 に答える
1

イメージに ID タグを追加します (まだ ID タグがない場合)。例:

<img src="pictures/apic.png" id="imageId" />

次に、次のように参照できます。

if (document.getElementById("imageId").src == "pictures/apic.png")
{
    ...
}

編集

ここでの実例: http://jsfiddle.net/2Wtkn/2/

于 2012-06-01T01:32:18.587 に答える