简体   繁体   English

HTML onclick功能不起作用

[英]HTML onclick function doesn't work

I am wondering why my onclick function doesn't work in this particular case. 我想知道为什么在这种情况下我的onclick函数不起作用。 My function is a pretty straightforward alert: 我的功能很简单:

function confirmEditProduct(editProductID){
      alert("Worked");
  }

This is my HTML code echoed in PHP: 这是我在PHP中回显的HTML代码:

    echo "
      <img src='$directory/$productImage' width='200px;'></img><br><br>
      <p>Product Name: <p><input type='text' id='editName' name='editName' style='width:400px;' value='$productName'/>
      <p>Category:
          <select class='form-control' id='editCat' style='width:200px;' disabled>
            <option value='Cacti'"; if ($productCategory == '1') echo " selected='selected'"; echo ">Cacti</option>
            <option value='Succulents'"; if ($productCategory == '2') echo " selected='selected'"; echo ">Succulents</option>
          </select>

      <p>Description: <p><textarea rows='4' cols='50' id='editDesc' name='editDesc'>$productDesc</textarea>
      <p>Price: <input type='text' id='editPrice' name='editPrice' value='$productPrice'/>
      <p>Image: <input class='input-group' type='file' id='editImage' name='editImage' accept='image/*' />
      <p>Stock: <input type='text' id='editStock' name='editStock' value='$productStock'/>
      <p>
      <input type='button' class='btn btn-success' id='$productID' onClick='confirmEditProduct($productID);' value='Edit Product' />


  ";

This snippet is part of a modal btw. 该代码段是模式btw的一部分。

Thank you to everyone who can help me out 谢谢所有能帮助我的人

This seems like it would be quite simple. 这似乎很简单。 Are you also echoing the Javascript? 您是否也在回应Javascript? If so, take an extra precaution and move it before the HTML code. 如果是这样,请格外小心,并将其移到HTML代码之前。 Some other things you may want to consider include making the input button into a straight up button and omitting the semicolon at the end of the onclick attribute. 您可能需要考虑的其他事项包括将输入按钮变成向上的按钮,并在onclick属性的末尾省略分号。 As for styling, you may want to put the value attribute after the type attribute. 至于样式,您可能需要将value属性放在type属性之后。 Hope this helps! 希望这可以帮助!

Edit: When I mean echoing the Javascript, make sure you have a <script src="main.js"> or the entire script within your echoed code. 编辑:当我要回显Javascript时,请确保您在回显的代码中包含<script src="main.js">或整个脚本。

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

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