The required HTML5 attribute trigger action with jQuery

I’m working on a prototype and I want to trigger an action after a submit but only if the required fields were filled.

I have this code

And also an input on my form using the ‘required’ attribute from HTML 5, like this

I want to trigger the action onclick only if the input was filled. I suppose I could do this by checking if the input was filled using jQuery, I was wondering if there is a simpler way

Well, I have actually found a solution, if anyone is going through the same problem, this is how I did it.

Instead of using “onclick” on the button tag I added an “onsubmit” (I had no clue this existed) event inside my form tag with my doSomething function, like this:

The function will only be called if the required inputs are filled, easy as that.

Thanks for the ones who tried to help anyway

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » The required HTML5 attribute trigger action with jQuery

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录