开发者

How to access a dynamically generated id of a submit button?

开发者 https://www.devze.com 2023-03-20 17:36 出处:网络
I have a submit button that has got a dynamically generated id. I want to make this button invisible once it was clicked once. At the same time another button which is invisible when the page loads wi

I have a submit button that has got a dynamically generated id. I want to make this button invisible once it was clicked once. At the same time another button which is invisible when the page loads will become visible instead.

But how can I reference the button I want to make invisible if the id is generated dynamically. I know the id always starts with the string 'product_'. Can I use left/subs开发者_开发知识库tring somehow to get control of this button? And how do I do it?


You could use the attribute starts with selector:

$("button[id^='product_']")


jQuery. You need to use live or delegate methods to access a dynamically generated ids

$('button[id^="product_"]').live('click', function(){alert("Hi");})


As an alternative you could give your two buttons unique css class names then you do not need to worry about the id's?

0

精彩评论

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

关注公众号