试图从JS变量中获取输入类型的id

时间:2016-12-07 10:45:55

标签: javascript html

var askQuitHTMLMessage = 
"<p><strong>Waar moet rekening mee worden gehouden bij het inplannen?</strong></p>" +
"<p><strong>Vul de volgende gegevens in om het resultaat te bepalen:</strong></p>" +
"<p><input class='inplan_var' data='ingeplande_uren' type='checkbox' name='medewerkers' id='aantmdw'><strong>&#09; Aantal uren resource</strong></input></p>" +
"<p><input class='inplan_var' data='ervaring' type='checkbox' name='medewerkers' id='ervaring'><strong>&#09; Ervaring</strong></input></form></p>" +
"<p><input class='inplan_var' data='expertise' type='checkbox' name='medewerkers' id='expertise'><strong>&#09; Expertise</strong></input></form></p>" +
"<p><input class='inplan_var' data='etc' type='checkbox' name='medewerkers' id='etc'><strong>&#09; Etcetera</strong></input></form></p>";

如何在javascript变量中获取其中一种输入类型的id?

2 个答案:

答案 0 :(得分:1)

这是一个解决方案,可以为您提供一系列ID

使用代码:https://jsbin.com/vejumo/edit?js,console

var askQuitHTMLMessage =  "...." // The string in the question

var tmpDiv = document.createElement('div');
tmpDiv.innerHTML = askQuitHTMLMessage;
var inputs = tmpDiv.querySelectorAll('input');
var ids = [];
inputs.forEach(function(input) {ids.push(input.id)})
console.log(ids);

答案 1 :(得分:0)

这应该适合你

  

$('input',$(askQuitHTMLMessage))。each(function(){console.log(“id:”+ $(this).attr('id'))})