简体   繁体   English

按钮onclick事件未在php中调用javascript函数

[英]Button onclick event is not calling javascript function in php

I'm calling a javascript function in php. 我在php中调用javascript函数。 it works when i call alert() method directly on click. 当我直接在单击时调用alert()方法时,它会起作用。 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 它适用于直接alert()方法

<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. 但是当我通过其他javascript函数调用它时,则将无法使用。

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

here is the javascript code. 这是JavaScript代码。

<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. 我认为您的属性“ id”和函数名称相同。 Please change the 'ID' and try. 请更改“ ID”并尝试。 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');\"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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