Jquery,每个使用$.each,不使用';无法获取变量的值
我使用Jquery向select添加一些选项,但是,这些选项并不总是相同的,因此,我使用一个名为material的变量来获取previus select的值,以知道应该添加哪些选项。 我不知道我的解释是否正确,这里有一些代码 我之前已经用我能得到的值的名称定义了变量Jquery,每个使用$.each,不使用';无法获取变量的值,jquery,select,add,each,options,Jquery,Select,Add,Each,Options,我使用Jquery向select添加一些选项,但是,这些选项并不总是相同的,因此,我使用一个名为material的变量来获取previus select的值,以知道应该添加哪些选项。 我不知道我的解释是否正确,这里有一些代码 我之前已经用我能得到的值的名称定义了变量 var name{ opt : 'text' } 问题是,尽管我在var material上得到了正确的值,但添加选项的函数不起作用 如果有人想查看整个代码: INDEX.php: <div class="col
var name{
opt : 'text'
}
问题是,尽管我在var material上得到了正确的值,但添加选项的函数不起作用
如果有人想查看整个代码:
INDEX.php:
<div class="col-xs-6 col-centered form-group">
<label for="facultad">Facultad</label>
<select class="form-control" name="facultad" id="selFac" required>
<option selected disabled hidden value="">---</option>
<option value="ingenieria">Ingeniería</option>
<option value="economia">Economía</option>
<option value="derecho">Derecho</option>
</select>
</div>
<div class="col-xs-6 col-centered form-group" id="divMateria">
<label for="materia">Materia</label>
<select class="form-control" name="materia" id="selMateria" required>
<option selected disabled hidden value="">---</option>
</select>
</div>
<div class="col-xs-12 col-centered form-group" id="divGrupo">
<label for="grupo">Grupo</label>
<select class="form-control" name="grupo" id="selGrupo" required>
<option selected disabled hidden value="">---</option>
</select>
</div>
多谢各位 首先:
materia = $("#selMateria").val();
$("#" + id).append(new Option($("#selMateria").val(), 'val'));
$.each(materia, function(val, text) {
$("#selGrupo").append(new Option(text, val));
});
在这种情况下,material
包含select的单个值<代码>$。在这种情况下,每个将仅对该单个值进行迭代。我想你想要的是:
materia = $("#selMateria").val();
$("#" + id).append(new Option($("#selMateria").val(), 'val'));
$.each($("#selMateria"), function(val, text) {
$("#selGrupo").append(new Option(text, val));
});
嘿,伙计,你能不能多发一些代码?这还不足以帮助你。谢谢你的回答。对于$.each,我想使用一个名为$(“#selmetria”).val()的变量进行迭代,正如您在,
$上看到的那样。每个
都需要一个数组作为第一个参数。在您的实际代码中,.val()
返回”
,因为您的--
具有未定义的值。好的,我确实需要阅读更多文档,lol。谢谢您,保罗!如果您能清楚地解释您的业务逻辑,我们将尽力帮助您。你的代码应该做什么?当与它交互时,您希望它做什么?
function addOpt(id, tipo, optDe) {
removeAllSel(id);
if (tipo == 'mat') {
switch (optDe)
{
case 'ingenieria':
$.each(matIng, function(val, text) {
$("#" + id).append(new Option(text, val));
});
break;
case 'economia':
$.each(matEco, function(val, text) {
$("#" + id).append(new Option(text, val));
});
break;
}
}
else if (tipo == 'gru') {
materia = $("#selMateria").val();
$("#" + id).append(new Option($("#selMateria").val(), 'val'));
$.each(materia, function(val, text) {
$("#selGrupo").append(new Option(text, val));
});
}
else {
removeAllSel(id);
}
}
materia = $("#selMateria").val();
$("#" + id).append(new Option($("#selMateria").val(), 'val'));
$.each(materia, function(val, text) {
$("#selGrupo").append(new Option(text, val));
});
materia = $("#selMateria").val();
$("#" + id).append(new Option($("#selMateria").val(), 'val'));
$.each($("#selMateria"), function(val, text) {
$("#selGrupo").append(new Option(text, val));
});