简体   繁体   中英

get select option value using $(document).ready function in jQuery from multiple dynamic record?

Here is my code

foreach($test as $val){

<select name="change" id="change-<?=$val['id']?>" data-id="<?php echo $val['id'];?>">
     <option value="">Select Value</option>
     <option value="1">one</option>
     <option value="2">two</option>
     <option value="3">three</option>                
 </select>
}

I want every different record blank value alert in every 3 min. so please help me. thanks!

 $(document).ready(function(e) {

    var change = $('#change').val();
    if (change == '') {
       alert('Please Select Shipping Destination!');
    }

});

For the interval you can use plain JavaScript's setInterval() method.

In order to find and iterate over all your select fields, you can use jQuery's " starts with " selector and .each() method.

This should work:

$(document).ready(function(e) {

    setInterval(function() {
        $("select[id^='change-']").each(function(i) {
            var value = $(this).val();
            if (value == '') {
                alert('Please Select Shipping Destination!');
            }
        });
    }, 3 * 60 * 1000);

});

Note that this will create an alert for every select that is empty. Probably not very user friendly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM