foo-baar
foo-baar

Reputation: 1104

How to find all drop downs which doesn't have any value selected?

I have a form which I submit manually (using JS) and it makes the querystring direct since it appends it with all the controls id (weather they have value or not) I read somewhere that I can avoid this by disabling the controls that have no values, before submitting the form.

Hence, In my form I have text box, checkbox and select (dropdowns), I am trying to write an inline query which will get all the select which have no option/values selected from its list :

This $('form').find('select option:selected[value!=""]') somewhat works but this $('form').find('select option:selected[value=""]') doesn't at all.

Any help would be appreciated.

Upvotes: 0

Views: 74

Answers (1)

Filippos Karapetis
Filippos Karapetis

Reputation: 4679

This is straightforward to do on form submission, by inspecting each element in the form. Make sure that you provide a way for users to actually re-enable the disabled form elements.

A working code would be:

$("#testForm").on("submit", function() {
    $(this).find('select option:selected[value=""]').parent().attr("disabled", "disabled");
    alert("ok");  // for debug purposes
    return false; // this stops the form from actually being submitted
});

Here's a working fiddle that demonstrates widget disabling:

http://jsfiddle.net/sw6v928m/3/

Edit: Updated to actually select disabled elements

Edit 2: Updated to compact the code a bit, after request from the OP

Upvotes: 1

Related Questions