简体   繁体   中英

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

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