如何使用 JavaScript 获取没有 id 的输入值

How to get input value with no id using JavaScript?

本文关键字:id 输入 何使用 JavaScript 获取      更新时间:2023-09-26

我有一个可编辑的DataTabe,在编辑模式下,生成的html完全如下所示:

<td><form><input autocomplete="off" name="value" ></form></td>

有文本框作为输入,我不需要获取此输入的值。但是,我不能给出id,因为没有DataTable的配置,我决定使用Javaascipt获取值。我已经尝试了许多不同的方法,如下所示,例如closest((,但无法获取值。有可能抓住它吗?

var $row = $(this).closest("tr"); 
$tds = $row.find("td");
你可以

使用document.querySelector

var input = document.querySelector('[name="value"]`);

或者,使用 jQuery,您也可以使用相同的选择器:

var input = $('[name="value"]');
var currentInput=null;
$("input").focus(function(e)
{
   currentInput=e.target or this;//here you can get currently editing textfeild or may be $(this) if this is wrong
});

然后你可以得到currentInput.value()

我看到你正在使用jQuery;你可以直接定位name属性,并使用.val((获取输入的值,如下所示:

$("input[name='value']").val();