简体   繁体   English

使用getElementsByTagName选择标记

[英]Using getElementsByTagName select tag

I'm using javascript to validate input text field values by selection column in my form. 我正在使用javascript通过表单中的选择列来验证输入文本字段值。 在此处输入图片说明

3 column Sample Point , Sampling Type and Low TPC . 3列采样点采样类型低TPC I want to make rules on selection column (Sampling type) for Equipment options and Personnel option . 我想在“ 设备”选项和“ 人事”选项的选择列(采样类型)上制定规则。 The Equipment option rules work fine, but if i select Personnel option on row 4, then the other rows value (Low TPC column) always executed Personnel option rules. 设备选项规则工作正常,但是如果我在第4行选择“ 人员”选项,则其他行值(“低TPC”列)将始终执行“ 人员”选项规则。

Here my code: 这是我的代码:

<div class="form-group">
   <label for="location" class="col-md-2 control-label" style="text-align:left;">location</label>    
   <div class="col-sm-3">
      <select name="location" id="location" class="form-control" onselect="setColor()">
         <option value=""></option>
         <option value="CMP">CMP</option>
         <option value="DRP">DRP</option>
      </select>
   </div>
</div>
[...]

<div class="row">
   <div class="col-md-12">
   <div class="table-responsive">
   [...]
      <td>
         <input type="text" name="sample_point[]" id="sample_point" size="17" value=""/></td>
      <td>
         <select name ="sampling_type[]" id="sampling_type">
            <option value=""></option>
            <option value="Equipment">Equipment</option>
            <option value="Personnel">Personnel</option>
            <option value="Environment">Environment</option>
         </select>
      </td>
      <td>
         <input type="text" name="low_tpc[]" id="low_tpc" onkeyup="setColor()" size="5" value=""/></td>
[...]



<script type="text/javascript">

    function setColor() {
        var dropdown = document.getElementById('location').value;

        switch (dropdown) {
            case 'DRP':
                var obj = document.getElementsByTagName('input');
                for(var i=0; i<obj.length; i++) {
                    if (obj[i].name == "sample_point[]") {
                        var sp = obj[i].value;
                    }
                    if (obj[i].name == "low_tpc[]") {
                        var low = obj[i].value;

                        var sels = document.getElementsByTagName("select");
                        for(var j=0; j<sels.length;j++) {
                            var sel = sels[j];
                            var type = sel.options[sel.selectedIndex].value;

                            switch (type) {
                                case 'Equipment':
                                    if(sp.match(/black/i)) {
                                        if(low > 5000) {
                                            obj[i].style.backgroundColor = "#fd6969";
                                        } else {
                                            obj[i].style.backgroundColor = "";
                                        }
                                    } else if(sp.match(/red/i) || sp.match(/blue/i)) {
                                        obj[i].style.backgroundColor = "";
                                    } else {
                                        if(low > 3200) {
                                            obj[i].style.backgroundColor = "#fd6969";
                                        } else {
                                            obj[i].style.backgroundColor = "";
                                        }
                                    }
                                    break; 
                                case 'Personnel':
                                    if(sp.match(/black/i)) {
                                        if(low > 50) {
                                            obj[i].style.backgroundColor = "#fd6969";
                                        } else {
                                            obj[i].style.backgroundColor = "";
                                        }
                                    } else {
                                        obj[i].style.backgroundColor = "";
                                    }
                                    break;   
                                default:
                                break;   
                            }  
                        }                                             
                    }                 
                }
            break;


            default:
            break;    
        }        
    }

    setColor();

</script>

How to make the selections to execute on each rules without affected other selection rules ? 如何使选择在每个规则上执行而不影响其他选择规则?

Your code loops through all the input elements, then for each input it is looping through all the select tags. 您的代码循环遍历所有input元素,然后对于每个input ,遍历所有select标签。 This is not only inefficient, but you also can't be sure that you are targeting elements withing the same row. 这不仅效率低下,而且还不能确定您要针对具有同一行的元素。

I'd suggest re-writing your function so that you are looping through all the rows, and then specifically targeting the controls within that row. 我建议重新编写函数,以便您遍历所有行,然后专门针对该行中的控件。 Here's an example: 这是一个例子:

 function setColor() { var dropdown = document.getElementById('location').value; switch (dropdown) { case 'DRP': var rows = document.getElementsByClassName("row"); //loop through all the rows [].forEach.call(rows, function(row) { //get inputs in the current row var sp = row.querySelector('[name="sample_point[]"]').value; var type = row.querySelector('[name="sampling_type[]"]').value; var obj = row.querySelector('[name="low_tpc[]"]'); var low = obj.value; switch (type) { case 'Equipment': if(sp.match(/black/i)) { if(low > 5000) { obj.style.backgroundColor = "#fd6969"; } else { obj.style.backgroundColor = ""; } } else if(sp.match(/red/i) || sp.match(/blue/i)) { obj.style.backgroundColor = ""; } else { if(low > 3200) { obj.style.backgroundColor = "#fd6969"; } else { obj.style.backgroundColor = ""; } } break; case 'Personnel': if(sp.match(/black/i)) { if(low > 50) { obj.style.backgroundColor = "#fd6969"; } else { obj.style.backgroundColor = ""; } } else { obj.style.backgroundColor = ""; } break; } }); } } function addRow() { var table = document.getElementById("table"); var newRow = table.querySelector(".row").cloneNode(true); table.appendChild(newRow); setColor(); } setColor(); 
 <div class="form-group"> <label for="location" class="col-md-2 control-label" style="text-align:left;">location</label> <div class="col-sm-3"> <select name="location" id="location" class="form-control" onselect="setColor()"> <option value=""></option> <option value="CMP">CMP</option> <option value="DRP" selected>DRP</option> </select> </div> </div> <input type="button" value="Add Row" onclick="addRow()" /> <div id="table"> <div class="row"> <div class="col-md-12"> <div class="table-responsive"> <td> <input type="text" name="sample_point[]" size="17" value="black"/> </td> <td> <select name ="sampling_type[]" onchange="setColor()"> <option value=""></option> <option value="Equipment" selected>Equipment</option> <option value="Personnel">Personnel</option> <option value="Environment">Environment</option> </select> </td> <td> <input type="text" name="low_tpc[]" onkeyup="setColor()" size="5" value="2500"/> </td> </div> </div> </div> <div class="row"> <div class="col-md-12"> <div class="table-responsive"> <td> <input type="text" name="sample_point[]" size="17" value="red"/> </td> <td> <select name ="sampling_type[]" onchange="setColor()"> <option value=""></option> <option value="Equipment" selected>Equipment</option> <option value="Personnel">Personnel</option> <option value="Environment">Environment</option> </select> </td> <td> <input type="text" name="low_tpc[]" onkeyup="setColor()" size="5" value="6500"/> </td> </div> </div> </div> <div class="row"> <div class="col-md-12"> <div class="table-responsive"> <td> <input type="text" name="sample_point[]" size="17" value="yellow"/></td> <td> <select name ="sampling_type[]" onchange="setColor()"> <option value=""></option> <option value="Equipment" selected>Equipment</option> <option value="Personnel">Personnel</option> <option value="Environment">Environment</option> </select> </td> <td> <input type="text" name="low_tpc[]" onkeyup="setColor()" size="5" value="1500"/> </td> </div> </div> </div> <div class="row"> <div class="col-md-12"> <div class="table-responsive"> <td> <input type="text" name="sample_point[]" size="17" value="black"/></td> <td> <select name ="sampling_type[]" onchange="setColor()"> <option value=""></option> <option value="Equipment">Equipment</option> <option value="Personnel" selected>Personnel</option> <option value="Environment">Environment</option> </select> </td> <td> <input type="text" name="low_tpc[]" onkeyup="setColor()" size="5" value="55"/> </td> </div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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