简体   繁体   English

JavaScript单击按钮1

[英]Javascript click button1

I have the following HTML code and I'm trying to make a JavaScript to click on this button 我有以下HTML代码,我正在尝试制作一个JavaScript以单击此按钮

<fieldset class="fields1"></fieldset>
<fieldset class="submit-buttons">
<some other button here >
<input class="button1" type="submit" value="Submit" name="post" tabindex="4" accesskey="f"></input>

I tried the following lines of code neither of which works: 我尝试了以下两行代码,但都不行:

$(".submit-buttons").children('input[name="post"]').click();


$("input[name=post]").click();

Is there any other way to click the button1? 还有其他方法可以单击按钮1吗? And is there a way to select the button by its tabindex or accesskey? 有没有一种方法可以通过其tabindex或accesskey选择按钮?

The click function is for event handling not triggering events. 单击功能用于事件处理而不触发事件。 If you want to trigger a click use trigger 如果要触发点击使用触发器

$("input[name=post]").trigger('click');

You forgot the close the <fieldset> tag 您忘记了关闭<fieldset>标记

Use following markup 使用以下标记

  <fieldset class="fields1"></fieldset>
  <fieldset class="submit-buttons">
    <input class="button1" type="submit" value="Submit" name="post" tabindex="4" accesskey="f">
  </fieldset>

Also, you would need to add a event handler callback inside click function 另外,您需要在click function内添加event handler回调

$(".submit-buttons").children('input[name="post"]').click(function(){
  alert("clicked")
});

and trigger the click event either manually or by jQuery's trigger() 并手动或通过jQuery的trigger()触发click事件

$("input[name=post]").trigger('click')

here's the demo 这是演示

First link up any jQuery version and then like 首先链接任何jQuery版本,然后喜欢

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

$("input[name=post]").click(function(){
    alert('dddd');
});

if onready click 如果准备就绪,请点击

$("input[name=post]").trigger('click');

<form action="#" method="post">
<input class="button1" type="submit" value="Submit" name="post" tabindex="4" accesskey="f"></input>
</form>

start <form> end </form>

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM