简体   繁体   English

检查选定的值并将输入字段更改为只读

[英]Check value selected and change input-field to read-only

When the option rond is selected in the select-list edit_type below I need input-field edit_maat_2 to be changed to readonly but nothing is happening.当在下面的选择列表edit_type中选择选项rond时,我需要将输入字段edit_maat_2更改为readonly但什么也没有发生。

This is my code:这是我的代码:

<select class="form-control" id="edit_type['.$i.']" name="edit_type" onclick="show_dim(this, '.$i.')">
<option ';if($row_table_1['type'] == 'rond') { echo 'selected="selected"';} echo 'value="as">Staf rond</option>
<option ';if($row_table_1['type'] == 'buis') { echo 'selected="selected"';} echo 'value="buis">Buis</option>
</select>

<input type="number" class="form-control" id="edit_maat_2['.$i.']" name="edit_maat_2" value="'.$row_table_1['maat_2'].'">

<script type="text/javascript">
function show_dim(selectVeld, nr)
{
    if(document.getElementById('edit_type['+nr+']').value == 'rond') {
        document.getElementById('edit_maat_2['+nr+']').attr('readonly','readonly'); }
}
</script>

$i is generated and is function ok. $i已生成并且功能正常。

When I check this is console mode I see this error: Uncaught ReferenceError: e is not defined I am not using the variable e any wehere(?)当我检查这是控制台模式时,我看到此错误: Uncaught ReferenceError: e is not defined我没有在任何地方使用变量e (?)

Any suggestions?有什么建议?

Several issue, many typos. 几个问题,很多错别字。

  • You have values "as" and "buis" - nowhere "rond" 您具有值“ as”和“ buis”-无处“ rond”
  • .attr is jQuery, not DOM .attr是jQuery,不是DOM

This is likely what you want 这可能是您想要的

 function setAccess() { var id = this.id.replace("edit_type", "edit_maat_2"), field = document.getElementById(id); if (this.value == "as") { field.setAttribute('readonly', 'readonly'); } else { field.removeAttribute('readonly'); } } window.addEventListener("load", function() { document.querySelectorAll("[id^=edit_type]").forEach(function(sel) { sel.addEventListener("change", setAccess); // Initialise the fields in case of PHP setting the selected attribute if ("createEvent" in document) { var evt = document.createEvent("HTMLEvents"); evt.initEvent("change", false, true); sel.dispatchEvent(evt); } else { sel.fireEvent("onchange"); } }); }) 
 <select class="form-control" id="edit_type[1]" name="edit_type"> <option value="as">Staf rond</option> <option value="buis">Buis</option> </select> 1 <input type="number" class="form-control" id="edit_maat_2[1]" name="edit_maat_2" value="1"> <select class="form-control" id="edit_type[2]" name="edit_type"> <option value="as">Staf rond</option> <option value="buis">Buis</option> </select> 2 <input type="number" class="form-control" id="edit_maat_2[2]" name="edit_maat_2" value="2"> 

It's a bit hard to tell but: 很难说,但是:

"edit_type['.$i.']"

will resolve to something like 将解决类似

"edit_type['.1.']"

whereas in your function you call you look for 而在您的函数中,您打电话寻找

'edit_type['+nr+']'

which becomes if nr == '.1.' 如果nr == '.1.' :

'edit_type[.1.]'

Note the missing ticks. 注意丢失的刻度线。 Also not you have a dot everywhere. 也不是到处都有点。 Either add the ticks in your function, or drop them and the dot (which will resolve to an operator without the tics) entirely. 要么在函数中添加刻度线,要么完全删除刻度线和点(这将解析为没有tic的运算符)。 This is adding ticks: 这会增加刻度线:

 function show_dim(selectVeld, nr) { if(document.getElementById("edit_type['"+nr+"']").value == 'rond') { document.getElementById("edit_maat_2['"+nr+"']").attr('readonly','readonly'); } } 
 <select class="form-control" id="edit_type['.1.']" name="edit_type" onclick="show_dim(this, '.1.')"> <option ';if($row_table_1['type'] == 'as') { echo 'selected="selected"';} echo 'value="as">Staf rond</option> <option ';if($row_table_1['type'] == 'buis') { echo 'selected="selected"';} echo 'value="buis">Buis</option> </select> <input type="number" class="form-control" id="edit_maat_2['.$i.']" name="edit_maat_2" value="'.$row_table_1['maat_2'].'"> 

But I think you just want 但是我想你只是想要

"edit_type[$i]"

and in the function argument 并在函数参数中

$i

instead of '.$i.' 而不是'.$i.' . If you were actually showing the final HTML result then the problem is the same, with $i everywhere as a string instead of 1 . 如果您实际上是在显示最终的HTML结果,那么问题是一样的,到处都是$i作为字符串而不是1

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

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