Disabling and Re-Enabling Dynamic Form Elements with JQuery

假装没事ソ 提交于 2019-12-12 02:05:37

问题


It's probably easiest to view this fiddle: http://jsfiddle.net/Ahfm7/4/

Only the agent and extension values in the dropdown have been implemented at this time.

I have encountered a couple of bugs I was hoping someone could help figure out.

  • The first is when you add a new row of dropdowns without selecting anything in the first and proceed to select an option in the second one, the form can be submitted.

  • Likewise if you pick an option in the first row, then add another and remove it, the submit button remains disabled.

I was just wondering if anyone knows how to fix this?


回答1:


I started cleaning up the code. I cut the 700+ lines of JS down to something more manageable for SO, but you should be able to understand the general idea.

http://jsfiddle.net/mattball/5ubTe/

In general:

  • Avoid re-querying the DOM by caching jQuery objects
  • Stay DRY by writing more, smaller functions which contain commonly reused logic (such as the enableSubmitButton function I added)
  • Don't forget to use var



回答2:


I finally almost have a complete working solution but with one bug:

http://jsfiddle.net/5ubTe/72/

Thanks for your advice.



来源:https://stackoverflow.com/questions/5790975/disabling-and-re-enabling-dynamic-form-elements-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!