jquery 选择器 - 识别每个组中的每个选择/选项值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7128072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:26:08  来源:igfitidea点击:

jquery selectors - identify each select/option value, in each Group

jqueryhtmljquery-selectors

提问by mustapha george

I want to identify (alert) each select/option value, in each Group. But I need a push...

我想识别(警报)每个组中的每个选择/选项值。但我需要推...

<script src="jquery.mobile/jquery.js"></script>

<div  id="groupA" class="preGroups">

    <div id="section-A1">
    <input name="SRPR1"  type="text">
        <select name='ACTC' id='preAction' > 
            <option value='007'>Stolen</option> 
            <option value='008'>Tampered</option> 
        </select>
    </div>

    <div id="section-A2">
    <input name="SRPR1"  type="text">
        <select name='ACTC' id='preAction' > 
            <option value='007'>Stolen</option> 
            <option value='008'>Tampered</option> 
        </select>
    </div>

</div>

<div  id="groupB" class="preGroups">

    <div id="section-B1">
    <input name="SRPR1"  type="text">
        <select name='ACTC' id='preAction' > 
            <option value='007'>Stolen</option> 
            <option value='008'>Tampered</option> 
        </select>
    </div>

    <div id="section-B2">
    <input name="SRPR1"  type="text">
        <select name='ACTC' id='preAction' > 
            <option value='007'>Stolen</option> 
            <option value='008'>Tampered</option> 
        </select>
    </div>

    <script>
$(document).ready(function()
{    
        // iterate through each group in groups
        groups = $('div[id^="group"]'); 
        $.each(groups, function(key, group) {
            fnValidateGroup($(group));      
        });

        // validation for reason codes in a specific Group
        function fnValidateGroup(currentGroup){

            selects = $(currentGroup).find('select[name="ACTC"]'); 
            $.each(selects, function(key, activity) {
                // show me activity seelctec in each case
                alert($(activity).val) ;
            });
        }
});     



</script>

*edit *output should look like:

*编辑 *输出应如下所示:

new Group
007
008
new Group
007
007

*edit *Here is the answer I came to with your help...

*编辑 *这是我在您的帮助下得出的答案...

$(document).ready(function()
{    
        // iterate through each group in groups
        groups = $('div[id^="group"]'); 
        $.each(groups, function() {
            console.log("New Group");
            fnValidateGroup(this);      
        });

        // validation for reason codes in a specific Group
        function fnValidateGroup(currentGroup){

            selects = $(currentGroup).find('select[name="ACTC"]'); 
            $.each(selects, function(key, activity) {
                // show me activity seelctec in each case
                console.log(($(activity)).val());
            });
        }
});     

回答by BZink

Something like this should work. What have you tried?

像这样的事情应该有效。你尝试过什么?

$("select").each(function(){
    alert($(this).attr("name"));
    $(this).children("option").each(function(){
        alert($(this).html());

    });
});

Or if you want to alert them in pairs (wasn't sure based on your question)

或者,如果您想成对提醒他们(根据您的问题不确定)

 $("select").each(function(){
        var selectName = $(this).attr("name");
        $(this).children("option").each(function(){
            alert(selectName + " " + $(this).html());

        });
    });

Here's with your group validation

这是您的小组验证

var groups = $("div[id^='group']");
groups.each(function(){
    var thisGroup = $(this).attr("id");
    $(this).find("option").each(function(){
         alert(thisGroup + " " + $(this).val());

    });


});

回答by Rafay

$("div").each(function(){

    $("select option",this).each(function(){

        alert($(this).val());

    });

});

http://jsfiddle.net/EPmLh/5/

http://jsfiddle.net/EPmLh/5/