简体   繁体   中英

How to get input value with no id using JavaScript?

I have an editable DataTabe and when edit mode, the generated html is exactly as shown below:

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

There is s TextBox as input and I need t get the value of this input. However, I cannot give id as there is no configuration of DataTable and I decided to get the value using Javaascipt. I have tried many different methods like closest() as shown below, but cannot get the value. Is it possible to grab it?

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

You might use document.querySelector :

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

Or, using jQuery, you could also use the same selector:

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

您的问题很难理解,可以直接使用jQuery定位name属性(我看到您使用的是jQuery,而不是JavaScript),并使用.val()来获取输入值,如下所示:

$("input[name='value']").val();
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
});

then you can get currentInput.value()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM