Calling Javascript from a html form

后端 未结 6 526
眼角桃花
眼角桃花 2021-01-30 06:18

I am basing my question and example on Jason\'s answer in this question

I am trying to avoid using an eventListener, and just to call handleClick

6条回答
  •  忘掉有多难
    2021-01-30 06:54

    There are a few things to change in your edited version:

    1. You've taken the suggestion of using document.myform['whichThing'] a bit too literally. Your form is named "aye", so the code to access the whichThing radio buttons should use that name: `document.aye['whichThing'].

    2. There's no such thing as an action attribute for the tag. Use onclick instead:

    3. Obtaining and cancelling an Event object in a browser is a very involved process. It varies a lot by browser type and version. IE and Firefox handle these things very differently, so a simple event.preventDefault() won't work... in fact, the event variable probably won't even be defined because this is an onclick handler from a tag. This is why Stephen above is trying so hard to suggest a framework. I realize you want to know the mechanics, and I recommend google for that. In this case, as a simple workaround, use return false in the onclick tag as in number 2 above (or return false from the function as stephen suggested).

    4. Because of #3, get rid of everything not the alert statement in your handler.

    The code should now look like:

    function handleClick()
          {
            alert("Favorite weird creature: "+getRadioButtonValue(document.aye['whichThing']));
          }
        
      
    
        
    Which of the following do you like best?

    Slithy toves

    Borogoves

    Mome raths

提交回复
热议问题