【发布时间】:2020-02-28 14:40:50
【问题描述】:
我有一个 HTML 表格,其中 ID 是从行计数器动态生成的:
$(table).find('tbody').append("<tr>name=\"tableRow\"</tr>"
+ "<td>"
+ "<select id=\"shapeSelect_" + rowCount + "></td>"
+ "<option onclick=\"sphereSelect()\" value=\"sphere\">Sphere</option>"
+ "<option onclick=\"cylinderSelect()\" value=\"cylinder\">Cylinder</option>"
+ "</select>"
+ "</td>"
+ "<td><input type=\"text\" id=\"altitude" + rowCount + "\"</td>"
+ "<td><input type=\"text\" name=\"maxAlt\" id=\"maxAltitude_" + rowCount + "></td>"
+ "</tr>"
选择球体时,我需要最大程度的输入。选择圆柱体后,应启用输入。
我找到的每个示例都非常简单,但需要确切知道 ID 是什么,以及它在我的代码中动态生成的位置。这是我发现的一个例子:
$(#maxAltitude).prop("disabled", true);
当 maxAltitude 更像:maxAltitude_10 时,我该怎么做?表中可能有 1-n 行,我需要专门禁用更改下拉选择的行中的最大高度。
我尝试过 jQuery 和 javascript,但似乎找不到一个好的方法:
<option onclick="shapeSelect()" value="sphere">Sphere</option>
<option onclick="shapeSelect()" value="cylinder">Cylinder</option>
function shapeSelect() {
var shapeSelects = document.getElementsByName("shapeSelect");
var maxAlts = document.getElementsByName("maxAlt");
for(var i = 0; i < shapeSelects.length; i++) {
switch(shapeSelects[i].value) {
case "sphere":
maxAlts[I].disabled = True;
break;
case "cylinder":
maxAlts[i].disabled = False;
}
}
}
使用上面的代码,我得到:SyntaxError: unexpected token: identifier 每当 shapeSelect() 被触发时。
我将代码修改如下:
<table class="myTable" id="myTable"></table>
$(table).find('tbody').append("<tr>name=\"tableRow\"</tr>"
+ "<td>"
+ "<select id=\"shapeSelect_" + rowCount + "></td>"
+ "<option value=\"sphere\">Sphere</option>"
+ "<option value=\"cylinder\">Cylinder</option>"
+ "</select>"
+ "</td>"
+ "<td><input type=\"text\" id=\"altitude_" + rowCount + "\"</td>"
+ "<td><input class=\"maxAltitudeInput\" type=\"text\" id=\"maxAltitude_" + rowCount + "\" disabled></td>"
+ "</tr>"
$('#myTable').on('change','.shapeSelector',function(){
var shouldDisableInput = $(this).val() === 'sphere';
$(this).closest('tr').find('.maxAltitudeInput').attr('disabled',shouldDisableInput);
}
当我更改形状选择器下拉菜单时,仍然没有任何反应。
编辑:
对命名不匹配表示歉意。我的开发机器在一个气隙网络上,我在 Stack Overflow 上手动干扰了帖子。 rowCount 变量正在另一个函数中创建和递增。为了简洁起见,我试图只将相关代码放在帖子中。
我 缺少 shapeSelector 中的一个类。那是缺失的环节。现在可以了!
【问题讨论】:
标签: javascript python jquery html django