简体   繁体   中英

Button onclick event is not calling javascript function in php

I'm calling a javascript function in php. it works when i call alert() method directly on click. here is my code

echo "<form action='' method='post' enctype='multipart/form-data' >
          <div class='fileupload fileupload-new' style='width: 250px;' data-provides='fileupload'><input type='hidden' value='' name=''>
          <div class='fileupload-preview thumbnail' style='width: 390px; height: 250px; '></div>
          <div>
         <span class='btn btn-file btn-success'><span class='fileupload-new'>Select image</span>
         <span class='fileupload-exists'>Change</span><input type='file' name=''></span>
      <button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic' onclick=\"alert('hi');\">Save</button>
       </div>
       </div>
       </form>";

this is the line which causing problem. it works for direct alert() method

<button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic' onclick=\"alert('hi');\">Save</button>

but when i call it through other javascript function, then this won't work.

<button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic' onclick=\"changeSitepic();\">Save</button>

here is the javascript code.

<script type="text/javascript">

function changeSitepic(){

     alert('hi');

        }

</script>

I unable to find where i'm doing wrong. please help.

I think your attribute 'id' and function name are same. Please change the 'ID' and try. Change your button like this and try. It may help you

 <button name='changepic' value='Save' class='btn btn-danger fileupload-exists' id='changeSitepic_id' onclick="return changeSitepic()">Save</button>

also there is no need to use double quotes and escape them. just use single quotes.

onclick='javascript:changeSitepic()'

在您的onclick中尝试一下

onclick=\"javascript:alert('hi');\"

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