【问题标题】:Input text fields string as a parameter in javascript在javascript中输入文本字段字符串作为参数
【发布时间】:2016-07-26 06:20:08
【问题描述】:

我有一些输入文本字段和一个 javascript。在此表单输入中,我想使用规范进行文本字段背景颜色验证:

  • location DRP,jns_sampling 设备,则 ha_tpc 最大输入值为 3200

  • location DRP,jns_sampling 设备,以及 sampling_point 输入字段包含“finish”字符串,则 ha_tpc 最大输入值为 100

通过以下 javascript 代码,如果我运行第一个规范,最大输入值 3200 不起作用,即使没有“完成”字符串和空字段,也始终执行第二个规范采样点字段。

<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="getWarning()">
         <option value="<?php echo $location;?>"><?php echo $location;?></option>
         <option value="WTP">WTP</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="sampling_point[]" id="sampling_point" size="17" value="<?php $a=set_value('sampling_point[0]'); echo $a; ?>"/></td>
      <td>
         <select name ="jns_sampling[]" id="jns_sampling">
            <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="ha_tpc[]" id="ha_tpc" onkeyup="getWarning()" size="5" value="<?php $a=set_value('ha_tpc[0]'); echo $a; ?>"/></td>
[...]

<script type="text/javascript">

function getWarning() {
    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 == "ha_tpc[]") {
                    var hatpc = obj[i].value;
                    var type = document.getElementById('jns_sampling').value;
                    switch (type) {
                        case 'Equipment':
                            var point = document.getElementById('sampling_point').value;
                            if (hatpc == '') {
                                obj[i].style.backgroundColor = "";
                            } else if (hatpc > 3200) {
                                obj[i].style.backgroundColor = "#E74C3C";
                            } else if (hatpc > 100) {
                                var subjns = point.match(/finish/);
                                obj[i].style.backgroundColor = "#E74C3C";
                            } else {
                                obj[i].style.backgroundColor = "";
                            };
                        break;
                        case 'Personnel':
                              if (hatpc == '') {
                                obj[i].style.backgroundColor = "";
                            } else if (hatpc > 700) {
                                obj[i].style.backgroundColor = "#E74C3C";
                            } else {
                                obj[i].style.backgroundColor = "";
                            };
                        break;    
                        default:
                        break;   
                    }                        
                } 
            }
        break;
        default:
        break;    
    }

}

</script>

【问题讨论】:

    标签: javascript html forms text input


    【解决方案1】:

    您将type 变量用于Places:

    var type = document.getElementById('jns_sampling').value;
    switch (type) {
        case 'Equipment':
        var type = document.getElementById('sampling_point').value;
    

    我不确定,但中途更改值看起来不是一个好主意。

    编辑 您使用带有括号 ('[]') 的 name 属性。尽量避免这种情况。

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 2018-08-05
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 2014-09-26
      相关资源
      最近更新 更多