简体   繁体   中英

Button Href to press submit button

I was just wondering how to go about getting a href to press a button.I want to get btn btn primary when pressed to: press the submit button.

<BODY>
<a class="btn btn-primary" href="#Home"><span class="fui-time"></span></a>
<a class="btn btn-primary" href="#School"><span class="fui-time"></span></a>
<form>
    <input type="submit" name="options" id="Home" value="1" />Home
    <input type="submit" name="options" id="School" value="2" />School
    <input type="submit"  name="options" id="Work" value="3" />Work

</form>


</BODY>

Thank-you.Also if you could also provide a in-depth explanation that would be great as I sort of new to javascript.

PS: Is there any way to do it without using the function onClick()

With jQuery:

$('.btn.btn-primary').click(function (ev) {
    $(this.href).click();
    ev.preventDefault();
});

Although I would say your markup and structure are quite strange, you want to have submit buttons and then links that cause those buttons to be clicked??

try this

    <BODY>
        <a class="btn btn-primary" href="#Home" onClick="formSubmit()"><span class="fui-time"></span></a> <a class="btn btn-primary" href="#School" onClick="formSubmit()"><span class="fui-time"></span></a>
        <form id="myform">
          <input type="submit" name="options" id="Home" value="1" />
          Home
          <input type="submit" name="options" id="School" value="2" />
          School
          <input type="submit"  name="options" id="Work" value="3" />
          Work
        </form>
        </BODY>
        <script type="text/javascript">
        function formSubmit(){
        $("#myform").submit();
        }
        </script>

You can execute javascript in a link like this <a href="javascript:alert('javascript go')">

Edit: as susheel pointed out this is not a good idea, its much better to just bind or use onClick to trigger the button click.

It would be better to do something like this:

<div onclick="document.myform.submit()"></div> (if you are submitting a form). There is no way to replace a button with another element with raw html. You will need to use javascript one way or another.

check the below fiddle:

http://jsfiddle.net/6Q4Va/3/

$('.btn.btn-primary').on('click',function () {
    $(this.href).click();
});

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