提问者:小点点

从生成的输入无线电获取值


所以im生成的输入字段很少,这就起作用了。

但我无法获得这个生成的输入字段的值。我可以让onchange工作,但我还是抓不到任何值。

所以我用jQuery尝试了很多方法,但都不奏效。我没有被迷惑。

 jQuery(document).on('change', function (){
        var id = "";
        alert( this.value);
}

jQuery(document).on('change', function (){
        var id = "";
        alert( jQuery(this).attr('id'));
}

这是生成的字段

<input class="mb-1 ml-2" style="transform: scale(1.3);" type="radio" id="input-'+counter+'">'

看起来就像

<input class="mb-1 ml-2" style="transform: scale(1.3);" type="radio" id="input-1">'

那也很管用。

但我无法获得该输入字段的id


共1个答案

匿名用户

this引用侦听器附加到的元素-这里是文档。这对您的代码失败,因为文档不是具有ID的文档;你想要输入。

使用更改处理程序的event参数来获取目标,即事件被调度到的元素:

null

jQuery(document).on('change', function(e) {
  console.log(e.target.value);
  console.log(e.target.id);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="mb-1 ml-2" style="transform: scale(1.3);" type="radio" id="input-1">'