简体   繁体   中英

Getting img.src value (path name)

I have a form for a registration page. The form contains an image that changes its src dependant on a situation. Within a script which activates upon submission of the form, I want the form to call an alert if that image has a particular src, so I need a way of retrieving and comparing the value.

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;
       }
    }

Here is the relative code in full:

    <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;"/>

用这个:

if (document.getElementById('FName_Status').getAttribute('src') == "pictures/apic.png")

Add an ID tag to your image (if it doesn't already have one), eg:

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

Then you can reference it as follows:

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

EDIT

Working example here: http://jsfiddle.net/2Wtkn/2/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM