简体   繁体   English

未通过HTML按钮onclick调用UDF Javascript

[英]UDF Javascript not called from HTML button onclick

I've been through it all looking high and low for an answer. 我已经经历了所有的高低寻找答案。 I've tried almost everything I found during the last 11 hours to no avail. 我已经尝试了过去11个小时内发现的几乎所有内容,但无济于事。 Here is my problem: 这是我的问题:

    <button type="button" onclick="xp2bank(100, 'att')">click</button> does not work,

HOWEVER, 然而,

    <button type="button" onclick="alert('this works')">click</button> does work.

What I am trying to do is make a UDF that can be called from many buttons by passing parameters to the function (It would be nice to know if there is an easier way of doing this.) The purpose of the function will be to update elements of $fetch[] based upon the value chosen after a check to insure their is enough held in either $fetch['bank_###'] or $fetch['exp_#####'] depending upon the users choice. 我正在尝试做的是通过将参数传递给函数来制作可以从许多按钮调用的UDF(很高兴知道是否有更简单的方法来执行此操作。)该函数的目的是更新$ fetch []的元素基于检查后选择的值,以确保根据用户的选择将其充分保留在$ fetch ['bank _ ###']或$ fetch ['exp _ #####']中。 Once the change is made the code will update the appropriate cell. 进行更改后,代码将更新相应的单元格。 When the user is done there will be a button to save their changes at which time the table will be updated. 完成用户操作后,将有一个按钮来保存他们的更改,此时将更新表。 The rest of the code will be a cake walk in the park once I get over this hurtle. 一旦我克服了这个麻烦,剩下的代码将是在公园散步。

My code is not pretty or commented yet, I've been in it so much I figure when I get it right I will make it pretty and well commented. 我的代码还不是很漂亮,也没有评论过,我一直在研究它,以至于当我弄清楚它的时候,我会把它变得漂亮且很好地评论。

below is a revised shortened snippet of the code. 以下是经过修改的简短代码段。

    <html>
    <head>
    <script>

      function bank2xp(howmuch, skill){
        switch(skill) {
            case 'att':
              alert('Attack ' + howmuch);
         }
      }

      function xp2bank(howmuch, skill){
        alert('Attack ' + howmuch);


      } 
    </script>

    </head>
    <body>
                <table class='table table-bordered'>
                    <thead>
                        <tr>
                          <th colspan="2">Priamary Combat Stats</th>
                          <th style="text-align: right" colspan="2">Banked XP: (value from array)</th>
                        </tr>
                        <tr>
                            <th >Stat</th>
                            <th style="text-align: center">Level</th>
                            <th colspan="2" style="text-align: right">Experiance Points</th>

                        </tr>
                    </thead>
                    <tr>
                        <td>Attack</td>
                        <td style="text-align: right">(value from array)</td>
                        <td></td>
                        <td style="text-align: right">(value from array)</td>

                    </tr>
                    <tr>
                        <td style="text-align: center" colspan="2">Send to bank</br>
                          <button type="button" onclick="xp2bank(100, 'att')">100</button>
                        </td>
                        <td style="text-align: center" colspan="2">Add from bank</br>
                          <button type="button" onclick="bank2xp(100, 'att')">100</button>
                        </td>
                    </tr>
                </table>
    </body>
    </html>

I beg you to forgive me, but I didn't check your code. 请您原谅,但我没有检查您的代码。 What I did is to isolate what you think it is not working, to prove that it should work. 我所做的就是隔离您认为不起作用的东西,以证明它应该起作用。

 function xp2bank(howmuch, skill){ switch(skill) { case 'att': alert('Attack ' + howmuch); break; case 'str': alert('Strentgh ' + howmuch); break; case 'def': alert('defence ' + howmuch); break; } } 
 <button type="button" onclick="xp2bank(100, 'att')">Attack 100</button> <button type="button" onclick="xp2bank(1000, 'str')">Strentgh 1,000</button> <button type="button" onclick="xp2bank(50, 'def')">Defence 50</button> 

Edited snippet. 编辑的代码段。

Ok, I think I found it... 好吧,我想我找到了...

function xp2bank(howmuch, skill)( ... )

It should be: 它应该是:

    function xp2bank(howmuch, skill){ 
switch(skill) {
  case 'att':
    alert('Attack ' + howmuch);
    break;
  case 'str':
    // ...
    break;
  case 'def':
    // ...
    break;
        }
     }

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

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