从JavaScript中动态创建的选择框中获取值
在这里,我编写了一些代码,用于使用JavaScript从动态选择框中获取值 代码如下:从JavaScript中动态创建的选择框中获取值,javascript,jquery,Javascript,Jquery,在这里,我编写了一些代码,用于使用JavaScript从动态选择框中获取值 代码如下: function getCategory(categoryval) { var category = categoryval; $.ajax({ type: 'GET', url: "childinfo.html?method=getCategory", async: false, success: function (respo
function getCategory(categoryval) {
var category = categoryval;
$.ajax({
type: 'GET',
url: "childinfo.html?method=getCategory",
async: false,
success: function (response) {
var result = $.parseJSON(response);
for (var j = 0; j < result.parentVOList.length; j++) {
$(category)
.append(
'<option value="' + result.parentVOList[j].classStreamID + '">' + result.parentVOList[j].category + '</option>');
}
}
});
}
var tt = document.getElementById("userRecords");
var tr2 = document.createElement("tr");
var td_stream = document.createElement("td");
var addtxt2 = document.createElement("select");
addtxt2.name = "stream";
addtxt2.id = "stream" ;
addtxt2.setAttribute("class", "stream");
getCategory(".stream");
td_stream.appendChild(addtxt2);
tr2.appendChild(td_stream);
tt.appendChild(tr2);
var pramoteBtn_tr1 = document.createElement("tr"); //Creating TR Tag for header
var pramoteBtn_td1 = document.createElement("td"); // Creating First TD
var br = document.createElement("br");
tt.appendChild(br);
var pramoteBtn = document.createElement("input");
pramoteBtn.setAttribute("type", "button");
pramoteBtn.setAttribute("value", "Pramote");
pramoteBtn.setAttribute("name", "pramote");
pramoteBtn.setAttribute("class", "pramote");
pramoteBtn_td1.appendChild(pramoteBtn);
pramoteBtn_tr1.appendChild(pramoteBtn_td1);
tt.appendChild(pramoteBtn_tr1);
但当我点击Pramote按钮时,值是未定义的
请给我任何获得流值的解决方案 由于您没有提供html,我不知道如何制作。但我会这样做:
var currentVal = 0;
function addOption(categoryId) {
$('#wrapper #' + categoryId).append('<option value="' + (currentVal++) + '">Some text</option>');
}
$(document).ready(function(){
$(document).on('click', '.promote', function(){
var values = [];
$.each($('select option:selected'), function(){
values.push($(this).val());
}
console.log(values);
}
});
<div id="#wrapper"></div>
使用addOption功能,您可以向指定的类别添加一些选项,然后单击.promote后,收集所有选定的值。value=$userRecords.stream.val???你最好发布呈现的HTML代码而不是这个不可读的js代码段。你能复制/通过生成的HTMLWait吗?你可以使用jQuery并设法想出一些像CreateElement意大利面代码一样糟糕的东西吗?addtxt2.id=stream+i;我从哪里来?@iMoses我没有被要求接受答案更好谢谢
var currentVal = 0;
function addOption(categoryId) {
$('#wrapper #' + categoryId).append('<option value="' + (currentVal++) + '">Some text</option>');
}
$(document).ready(function(){
$(document).on('click', '.promote', function(){
var values = [];
$.each($('select option:selected'), function(){
values.push($(this).val());
}
console.log(values);
}
});
<div id="#wrapper"></div>