最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery do something when select options' values are not emptyNULL - Stack Overflow

programmeradmin4浏览0评论

Here's a scenario. Supposing I have multiple <select>'s on a page (can be 0,1,2,3,4 or more) and I want some piece of code to fire when the option values are not equal to "" or NULL. Is there a quick fire method in jQuery to find this? The selects will be in a <div> tag with class of class="variations" but no useful classes or id's for the selects themselves are present as they will be dynamically generated based on the current page/product id.

I can get it to work when one or the other is not equal to "" or NULL but I just can't seem to get it to fire when only both are selected (and then fire again when both are unselected, if that makes sense).

Here's a scenario. Supposing I have multiple <select>'s on a page (can be 0,1,2,3,4 or more) and I want some piece of code to fire when the option values are not equal to "" or NULL. Is there a quick fire method in jQuery to find this? The selects will be in a <div> tag with class of class="variations" but no useful classes or id's for the selects themselves are present as they will be dynamically generated based on the current page/product id.

I can get it to work when one or the other is not equal to "" or NULL but I just can't seem to get it to fire when only both are selected (and then fire again when both are unselected, if that makes sense).

Share Improve this question asked Sep 3, 2013 at 20:04 ScottMcGreadyScottMcGready 1,6222 gold badges24 silver badges33 bronze badges 4
  • 3 Could you provide your code, preferably in jsfiddle? – IgnasK Commented Sep 3, 2013 at 20:07
  • 2 Just bind the change event to all the selects and then within it, check all the values. – j08691 Commented Sep 3, 2013 at 20:07
  • Sounds good but how would I go about doing that? Like I said I can get it to fire when 1 is changed but not when they're all changed to something with a value other than "". – ScottMcGready Commented Sep 3, 2013 at 20:13
  • @ScottMcGready The event will fire every time any change occurs. Inside the handler, check the values of each of the selects to see if they are all selected. – Jason P Commented Sep 3, 2013 at 20:16
Add a ment  | 

4 Answers 4

Reset to default 6

You may try this

$('.variations').on('change', 'select', function(){
    if(this.value){
        // do something
    }
});

DEMO.

The answer posted by Sheikh is good, but this is another option:

http://jsfiddle/Jun5z/

HTML:

<select class="mySelect">
    <option value="">Please select an option</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

<select class="mySelect">
    <option value="">Please select an option</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>

<button class="myButton">Submit</button>

JavaScript:

$(document).ready(function(){
    $('.myButton').click(function() {
        $(".mySelect option:selected").each(function() {
            // do something if it's not empty
            if (this.value != '')
                console.log(this.text);
        });
    });
});

Slightly confused by your question, but would this be of any help?

var $selects = $('.variations > select')

// Attach event handler that will only fire once
$selects.one('change', checkSelectValues);

var checkSelectValues = function($event) {
  var numEmptySelects = 0;
  $selects.each(function(index, element){
    var val = $(element).val();
    if (val === '' || val == null) {
      numEmptySelects++;
    }
  });

  performActionBasedOnNumEmptySelects(numEmptySelects);

  // Re-attach event handler
  $selects.one('change', checkSelectValues);
};

var performActionBasedOnNumEmptySelects = function(numEmpty) {
  // Whatever....
};

If you only want something to occur when all are selected, or none are selected, you can use this:

http://jsfiddle/dK8yH/

$('.variations').on('change', 'select', function () {
    var allItems = $('.variations select');
    var nonSelectedItems = allItems.filter(function () {
        return $(this).val() === "";
    });

    if (nonSelectedItems.length == 0) {
        console.log('all are selected');
    } else if (nonSelectedItems.length == allItems.length) { 
        console.log('none are selected');   
    } else {
        console.log('some are selected');   
    }
});
发布评论

评论列表(0)

  1. 暂无评论