我正在尝试对此示例进行一些 JavaScript 工作:
var str= '<td><input id="product" type="checkbox" name="product' + i + '" value="Yes" /> new product?</td>';
str = str+'<td><input type="text"class="form-control" id="number ' + i + '" name="number' + i + '" placeholder="GTIN"</td>'
<script>
$(document).ready(function () {
var ckbox = $('#product');
$('input').on('click', function () {
if (ckbox.is(':checked')) {
document.getElementById("number").readOnly = true;
document.getElementById("number").value = "";
} else {
document.getElementById("number").readOnly = false;
}
});
});
</script>
这似乎仅当我的输入复选框位于字符串生成器之外时才有效
最佳答案
如果我正确理解你的问题,你正在尝试检查作为字符串创建的输入是否被检查。为此,您可以尝试这样的操作:
HTML
<div id="number">
<table id='mytable'>
</table>
</div>
JS
var str;
for(var i = 0; i < 4; i++){
str = '<td><input id="product'+i+'" type="checkbox" checked name="product' + i + '" value="Yes" />new product?';
str = str+'<td><input type="text"class="form-control" id="number ' + i + '" name="number' + i + '" placeholder="GTIN" data-checkbox-id="product'+i+'"></td>'
$("#mytable").append(str);
}
$("[id*='product']").on("click", function(){
var this_id = $(this).attr("id");
if( $(this).is(":checked") )
$("input[data-checkbox-id='"+this_id+"']").removeAttr("disabled");
else
$("input[data-checkbox-id='"+this_id+"']").attr("disabled", "disabled");
});
检查 fiddle :https://jsfiddle.net/koky7zj1/11/
这就是您要找的吗?
编辑
查看更新的代码。我很高兴能提供帮助! :)
关于javascript - 如何在javascript中调用输入id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40467111/