简体   繁体   English

页面加载后自动单击按钮

[英]Automatically click button once the page loads

I have a starting page with some text, where when I click show images button in starting page, It will redirect to another page and displays images.我有一个带有一些文本的起始页面,当我单击起始页面中的显示图像按钮时,它将重定向到另一个页面并显示图像。

<form>
<select name="images" id="images">
        
        <option value="cars">Car Images</option>
        <option value="cycles">Cycle Images</option>
        <option value="bike">Bike Images</option>
</select>
<input id="getimages" type="submit" name="submit" value="Get Images">
</form>

when I select any of the option and click Get Images button, only then it will show images accordingly.当我 select 任何选项并单击获取图像按钮时,它才会相应地显示图像。 But I want to display images with automatically selected option(first) when page loads with out clicking Get Images button.但是我想在页面加载时显示带有自动选择选项(第一个)的图像,而无需单击“获取图像”按钮。 I tried using我尝试使用

document.getElementById("getimages").click()

but the page is reloading again and again.但页面一次又一次地重新加载。 How can I solve this with JS/JQuery.我怎样才能用 JS/JQuery 解决这个问题。 I hope you understand the above issue.我希望你理解上面的问题。 If not please let me know.如果没有,请告诉我。 Thanks.谢谢。

If you auto click the submit button when page loads without any condition it will create an infinite loop.如果您在页面加载时自动单击submit按钮而没有任何条件,它将创建一个无限循环。

You can use select onchange event.您可以使用 select onchange事件。

<select name="images" id="images" onchange="document.getElementById("getimages").click()">

But it's not the optimum solution.但这不是最佳解决方案。 I recommend you to use an ajax request to load images without any page refresh.我建议您使用 ajax 请求来加载图像而无需任何页面刷新。

I don't know if I understood you well but if yes then this might help you: I don't have your JS code but I can guess that you have an event runned when clicked the input, so you could just add the same event which would run when the document loads.我不知道我是否理解你,但如果是,那么这可能会对你有所帮助:我没有你的 JS 代码,但我猜你在单击输入时运行了一个事件,所以你可以添加相同的事件它将在文档加载时运行。 I mean something like that我的意思是这样的

const showImages = () => {
   // the code that shows the images
}

document.querySelector("#getimages").addEventListener("click", showImages())
document.addEventListener("load", showImages())

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

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