javascript - select all form elements that are selected as well as non-selectable form elements -
i have markup looks this:
<form> <select class="form-control" id="follower_id" name="event[task_followers_attributes][0][follower_id]"> <option value=""></option> <option data-typeahead-associated-id="1" value="1" selected>administrator</option> <option data-typeahead-associated-id="2" value="2">marketing guy</option> </select> <input type="hidden" data-typeahead-associated-id="2" name="event[followers_attributes][1][id]" value="2"> </form>
i want find form elements have data attribute "data-typeahead-associated-id". however, if form element form element selectable (e.g. select, radio button), want data-typeahead-associated-id 1 selected. since input fields not have multiple options, want input fields match "data-typeahead-associated-id" attribute.
therefore, in above example want select option value 1, not 2. , want input field value 2.
i thought solution, appears working:
$('form').find('[data-typeahead-associated-id]').not('option:not(:selected),input[type="radio"]:not(:checked),input[type="checkbox"]:not(:checked)')
but there more stable, elegant solution?
i not know if solution more efficient. notice have used double not instruction it's affermative instruction @ end.
my solution split 2 simpler instructions.
first select elements [data-typeahead-associated-id]
second filter items liking (:selected
, :checked
, :input
)
in way have more control on elements.
try:
var elements = $('form').find('[data-typeahead-associated-id]'); var result = elements.filter(":selected, :checked, :input"); //following display results $.each(result, function(index, value){ $('#result').append("<p>index: " + index + " | element: " + value + " | value: " + $(value).val() +"</p>");});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <select class="form-control" id="follower_id" name="event[task_followers_attributes][0][follower_id]"> <option value=""></option> <option data-typeahead-associated-id="1" value="1" selected>administrator</option> <option data-typeahead-associated-id="2" value="2" >marketing guy</option> </select> <input type="hidden" data-typeahead-associated-id="2" name="event[followers_attributes][1][id]" value="2"> </form> <!-- following display results --> <div id="result"> </div>
Comments
Post a Comment