toggle “required” attribute (of a hidden field) wh

2019-08-31 14:40发布

I'm trying to do two things:

  1. Show a hidden field if an option is selected, and hide that field if not (I have this part working - although if I use a variable it doesn't work)

  2. If the option is selected, add a required attribute to the field that was displayed. When another option is selected, remove the required attribute.

Here's a fiddle link:

http://jsfiddle.net/tucsonlabs/QCY2Q/

1条回答
走好不送
2楼-- · 2019-08-31 15:01

if ($("#aCard").filter(":selected")) this will always pass through because jQuery always return an object and anything non zero or not null or undefined passes through if block.

Use this if ($("#aCard").filter(":selected").length > 0), you can even use variable to show the required element in your fiddle take a look.

http://jsfiddle.net/QCY2Q/1/

查看更多
登录 后发表回答