开发者

No repeat selectors with jQuery

I have a drop-down selector and need to load other parts of the form based on selection.

I repeat this selector below a few times and need to make sure that no same options are selected. I suppose I need to run through the selected options of all mySelector and if the option is selected show some error message. Not sure how.

Here's what I've got so far:

$('.mySelector').change(function(){
    var selectForm = '#' + $(this).val();
    $('div').next('.fLoad').load('formParts.html ' + selectForm );
});

<select class="mySelector">
   <option value="selectOne">Select one</option>
   <option value="o1">Car details</option>
   <option value="o2">Boat details</option>
   <option value="o3">Train details</option>
   <option value="o4">Bike details</option>
   <option value="o5">Sub details</option>
</select>

<div class='fLoad'> </div>
   <option value="selectOne">Select one</option>
   <option value="o1">Car details</option>
   <option value="o2">Boat details</option>
   <option value="o3">Train details</option>
   <option value="o4">Bike details</option>
   <option value="o5">Sub details</option>

<div class='fLoad'> </div>

<select class="mySelector">
   <option value="selectOne">Sel开发者_Go百科ect one</option>
   <option value="o1">Car details</option>
   <option value="o2">Boat details</option>
   <option value="o3">Train details</option>
   <option value="o4">Bike details</option>
   <option value="o5">Sub details</option>
</select>

<div class='fLoad'> </div>


Make a dictionary of values to bools, that way you only have to loop over the options once.

var alreadyUsed = {};

$("select").each(function(){
    var thisVal = $(this).val();
    if(alreadyUsed[thisVal]){
        // found a dupe
    }

    alreadyUsed[thisVal] = true;
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜