javascript - 如何在javascript中调用输入id

标签 javascript jquery html input

我正在尝试对此示例进行一些 JavaScript 工作:

var str= '<td><input id="product" type="checkbox" name="product' + i + '" value="Yes" />&nbsp; 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/

相关文章:

javascript - 如何让 Chrome 下载 API 等到下载结束?

javascript - 如何在本地测试 Backbone 路由器哈希?

jquery - 确定 html 表中是否存在精确行

html - 绝对定位到 img 的 Div 不应移动

javascript - 调整图像大小后,减少由fabric.js序列化的JSON的大小

javascript - 时刻js UTC+00 :00 giving 1 hr extra

jquery - 如果在外部单击则隐藏/切换

javascript - 两个元素的鼠标事件,一个在另一个之上

javascript - 如何触发 HTML Select onClick 事件(激活带有选项的选择下拉列表)

javascript url 值到多个字段