开发者

can I click one button and use the action of another?

I have two buttons, and I want to apply one action to another. For example.

<form>

<inpu开发者_如何学Pythont type="submit" />
</form>

<input type="submit" />

I want to make the second button submit the form, despite being outside of the form.


You should make them of type button, and give your form an id.

Markup:

<form id="myForm">
  <---STUFF---->
<input type='button' id='otherButton'>
</form>

<input type='button' id='someButton'>

jQuery:

$('#someButton').click(function() { $('#myForm').submit(); });


$(':input:last').click(function()
{
   $('form').submit();
});


// enable form submit on the second <input>
$('input[type=submit]').click(function(){
      $('form').submit();
});

// disable the inner <input>
$('form input[type=submit]').click(function(){
      return false;
});

Something like this should do your trick.


If you want the second button to do everything the first can do (not just submit), you can try something like this:

html

<form>
<input id="insideButton" type="submit" />
</form>

<input id="outsideButton" type="submit" />

jsJQUERY

$("#outsideButton").click(function() {
    $("#insideButton").trigger("click");
});


you can using jQuery $("#test").click() if you have jQuery instead of the document... stuff below or the way below will work without jQuery

<form name="testForm1">
<input type="submit" id="test" />
</form>
<!-- use this when not in a form: -->
<button onclick="button1_click();" id="button1"></button>

In your head tag

<script type="text/javascript">
function button1_click(){
  document.forms.testForm1.submit();
}
</script>

Changed for the users that put performance under clean code

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜