javascript 获取div中的所有子输入元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25302684/
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-10-28 04:20:09  来源:igfitidea点击:

Getting all child input elements within a div

javascript

提问by samsu

I am trying to get all the values of the input fields. The issue is all of the <input type=radio/>are dynamic and can increase or decrease at any time.

我正在尝试获取输入字段的所有值。问题是所有这些<input type=radio/>都是动态的,可以随时增加或减少。

So I am starting with the main DI and going from there. The problem I have now is I am not getting the input radio buttons values.

所以我从主要的 DI 开始,然后从那里开始。我现在遇到的问题是我没有获得输入单选按钮值。

So here are the steps I am intending to accomplish:

所以这里是我打算完成的步骤:

  1. If any radio button is selected, pass its value to the checkbox value,
  2. If the radio button is selected and the checkbox is not selected, do notpass to the checkbox value
  1. 如果选择了任何单选按钮,则将其值传递给复选框值,
  2. 如果单选按钮被选中而复选框未被选中,则不传递给复选框值

I am looking for a solution in JavaScript only - do not use jQuery

我只是在寻找 JavaScript 的解决方案 -不要使用 jQuery

Here is my jsFiddle code

这是我的jsFiddle 代码

HTML

HTML

<div style="display: block;" id="mymainDiv" class="fullFloat">

            <input type="hidden" value="1" id="startIdxShMdeCarWisevId" name="startIdxShMdeCarWise">
                <div class="subTitle">UPS<a class="fRight" onclick="localG('10',false,0,false,'UPS','1','$');" href="javascript:void(0);">Show Prices</a></div>
                <div style="display:none;" id="Wheel_UPS"><div class="loadingcheckout"></div></div>
                    <div id="Price_UPS">

                    </div>

                <div class="wrapLeft wrapClear">

                            <div class="wrapleft">
                                <label class="">
                                    <input type="radio" value="11098" id="deliveryMethodId_1" name="deliveryMethodId" class="section" data-mask="" data-rev="" data-rel="false" data-carrier=""> 



                                    <span>
                                        UPS Ground (Order by 9:30 PM EST)
                                    </span>
                                    <div class="wrapRight">
                                        <div id="UPS_11098">
                                        </div>
                                    </div>
                                </label>
                            </div>

                        <input type="text" value="1" id="UPS">

                    </div>

            <input type="hidden" value="2" id="startIdxShMdeCarWisevId" name="startIdxShMdeCarWise">
                <div class="subTitle">Standard<a class="fRight" onclick="localG('20',false,0,false,'Standard','2','$');" href="javascript:void(0);">Show Prices</a></div>
                <div style="display:none;" id="Wheel_Standard"><div class="loadingcheckout"></div></div>
                    <div id="Price_Standard">

                    </div>
                <div class="wrapLeft wrapClear">

                            <div class="wrapleft">
                                <label class="">
                                    <input type="radio" value="11117" id="deliveryMethodId_2" name="deliveryMethodId" class="section" data-mask="" data-rev="" data-rel="false" data-carrier=""> 



                                    <span>
                                        Standard Delivery - 2-3 Day Delivery at Ground Rate (Order by 9:30 PM EST) 
                                    </span>
                                    <div class="wrapRight">
                                        <div id="Standard_11117">
                                        </div>
                                    </div>
                                </label>
                            </div>

                        <input type="text" value="1" id="Standard">

                    </div>

            <input type="hidden" value="3" id="startIdxShMdeCarWisevId" name="startIdxShMdeCarWise">
                <div class="subTitle">FedEx<a class="fRight" onclick="localG('190',false,0,false,'FedEx','3','$');" href="javascript:void(0);">Show Prices</a></div>
                <div style="display:none;" id="Wheel_FedEx"><div class="loadingcheckout"></div></div>
                    <div id="Price_FedEx">

                    </div>
                <div class="wrapLeft wrapClear">

                            <div class="wrapleft">
                                <label class="">
                                    <input type="radio" value="11088" id="deliveryMethodId_3" name="deliveryMethodId" class="section" data-mask="" data-rev="" data-rel="false" data-carrier=""> 



                                    <span>
                                        FedEx Ground (Order by 8:00 PM EST)
                                    </span>
                                    <div class="wrapRight">
                                        <div id="FedEx_11088">
                                        </div>
                                    </div>
                                </label>
                            </div>

                        <input type="text" value="1" id="FedEx">

                    </div>

        </div>
<input type="checkbox" name="shipmode" id="shipmode" value="" onclick="getpref('mymainDiv');">Get Value

JS Code

JS代码

This executes when the checkbox is clicked:

当复选框被点击时执行:

function getpref(val) {
    var wr = document.getElementById(val);
    childElements = wr.childNodes;
    //alert(childElements);
    for(var i = childElements.length-1; i>=0; i--){
        var elem = childElements[i];
        console.log(elem.id);
        if(elem.id && elem.id.indexOf(val+'_')==0){
            elem.style.display = 'block';
        }
    }
    //alert(val);
}

采纳答案by Dominic Zukiewicz

You can use getElementsByNameto get you all of the radio buttons by name='deliveryMethodId'and then go from there:

您可以使用getElementsByName让您获得所有单选按钮name='deliveryMethodId',然后从那里开始:

function getpref(val) {
    var radioButtons = document.getElementById(val).getElementsByName("deliveryMethodId");

    for(var i = radioButtons.length-1; i>=0; i--)
    {
        var radioButton = radioButtons[i];  

        if(radioButton.checked)
            console.log(radioButton.id + " is selected ");
    }
}

回答by Volune

You can directly access input nodes in your DIV with getElementsByTagName

您可以直接访问 DIV 中的输入节点 getElementsByTagName

function getpref(val) {
    var divNode = document.getElementById(val);
    var inputNodes = divNode.getElementsByTagName('INPUT');
    for(var i = 0; i < inputNodes.length; ++i){
        var inputNode = inputNodes[i];
        if(inputNode.type == 'radio') {
            //Do whatever you want
            if(inputNode.checked) {
                //Do whatever you want
            }
        }
    }
}

Example: http://jsfiddle.net/88vp0jLw/1/

示例:http: //jsfiddle.net/88vp0jLw/1/