简体   繁体   English

动态生成的选择标签选项未由jquery过滤

[英]Dynamically generated select tag option not filtered by jquery

I dynamically generate the select tag using jstl like below.But I am failed to select the option in jquery. 我动态生成使用选择标签jstl像below.But我没有选择在jQuery的选项。

JSP: JSP:

<c:forEach var="p" items="${model.multiphase}" varStatus="row">
    <select class="form-control status" name="status">
        <option value="Notyetstarted">Not Yet Started</option>
        <option value="Inprogress">InProgress</option>
        <option value="Onhold">OnHold</option>
        <option value="Closed">Closed</option>
    </select>
    <input type="hidden" id="upstatus_${row.index}" value="<c:out value='${p.getProjectPhase().getPhaseStatus()}'/>" />
</c:forEach>

the above jstl generates the below html 上面的jstl生成下面的html

<select class="form-control status" name="status">
        <option value="Notyetstarted">Not Yet Started</option>
        <option value="Inprogress">InProgress</option>
        <option value="Onhold">OnHold</option>
        <option value="Closed">Closed</option>
    </select>
    <input type="hidden" id="upstatus_0" value="Closed" />
 <select class="form-control status" name="status">
        <option value="Notyetstarted">Not Yet Started</option>
        <option value="Inprogress">InProgress</option>
        <option value="Onhold">OnHold</option>
        <option value="Closed">Closed</option>
    </select>
    <input type="hidden" id="upstatus_1" value="Onhold" />

I have used below jquery for selecting the option. 我已经使用下面的jQuery来选择选项。

$(".status option").each(function () {
    alert("fd");

    $(this).find("option").filter(function () {

        return $(this).val() == $("#upstatus_" + index).val();

    }).prop('selected', true);
});

How to preselect the value? 如何预选值? Any help will be greatly Appreciated!!! 任何帮助将不胜感激!!!

change to this: 更改为此:

$(".status").each(function () {
    var that = $(this);
    $(this).find('option').filter(function (index) {
        console.log($(this).parent().next(":hidden").val());
        return $(this).val() === that.next(":hidden").val()
    }).prop('selected', true);
});

Sample Demo 样本演示

Updated Sample Demo 更新了示例演示


-> : jquery not enters in the alert. -> : jQuery不会进入警报。

because you have .each() iteration on the option not on the select element. 因为您对选项而不是select元素具有.each()迭代。


Although you can change to this also: 尽管您也可以更改为:

$(".status").each(function (i) { // add a index "i" here
    $(this).find('option').filter(function () {
        return $(this).val() === $("#upstatus_"+i).val() // use that here
    }).prop('selected', true);
});

Sample for this. 样品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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