i want a anchor should act like and input type submit button

后端 未结 5 1837
忘掉有多难
忘掉有多难 2020-12-18 05:17

i want a anchor should act like and input type submit button. i am using a jquery plugin library that actually uses input type submit but i have styled my buttons on anchors

相关标签:
5条回答
  • 2020-12-18 05:23

    Since you're using jQuery, just use $() to select the form element, and call submit on it; hook all this up to the anchor via $() to find the anchor and click to hook up the handler:

    $("selector_for_the_anchor").click(function() {
        $("selector_for_the_form").submit();
        return false;
    });
    

    Probably best to return false; to cancel the click on the anchor.


    Off-topic: But note that this makes your page completely unusable without JavaScript, as well as making it confusing even for JavaScript-enabled browsers employed by users requiring assistive technologies (screenreaders, etc.). It makes the markup completely un-semantic. But since you'd said quite clearly that this was what you wanted to do...

    0 讨论(0)
  • 2020-12-18 05:31

    you can use input of type image (it works as a submit button for a form) or in jquery:

    $("a").click(function(event){
       event.preventDefault();
       $('form').submit();
    })
    
    0 讨论(0)
  • 2020-12-18 05:32

    If you want an anchor tag to act like a button just do this

    <!--YOUR FORM-->
    <form id="submit_this">.....</form>
    <a id="fakeanchor" href="#"></a>
    
    <script>
        $("a#fakeanchor").click(function()
        {
        $("#submit_this").submit();
        return false;
        });
    </script>
    
    0 讨论(0)
  • 2020-12-18 05:37
    <a id='anchor' href="javascript to submit the form" ></a>
    

    now you can use jquery to add an event handler

    $('#anchor').click(function (e) {
      // do some work
    
     // prevent the default anchor behaviour
     e.preventDefault();
    })
    

    now you can style your anchor as you wish and it will act as a regular button

    0 讨论(0)
  • 2020-12-18 05:40

    And what about:

    <form id="formOne">
            ...
       <a href="..." onclick="formOne.submit()">link here</a>
    </form>
    
    0 讨论(0)
提交回复
热议问题