Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择框在工作不正常后初始化_Javascript_Jquery_Css_Twitter Bootstrap_Twitter Bootstrap 3 - Fatal编程技术网

Javascript 选择框在工作不正常后初始化

Javascript 选择框在工作不正常后初始化,javascript,jquery,css,twitter-bootstrap,twitter-bootstrap-3,Javascript,Jquery,Css,Twitter Bootstrap,Twitter Bootstrap 3,我试图得到一个选择框的值,但它总是给我第一个值 开始时,select为空。我按下一个按钮,显示模式并加载选择框的信息。我想问题来了,因为我没有加载选择选项,直到按下按钮,因为我尝试了另一个选择,它工作得很好 这是模式: <div class="modal fade" id="myModalPremios" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div

我试图得到一个选择框的值,但它总是给我第一个值

开始时,select为空。我按下一个按钮,显示模式并加载选择框的信息。我想问题来了,因为我没有加载选择选项,直到按下按钮,因为我尝试了另一个选择,它工作得很好

这是模式:

<div class="modal fade" id="myModalPremios" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Premios</h4>
            </div>
            <div id="premios" class="modal-body">
                <jsp:include page="../frag/premios-list-frag.jsp"/>
            </div>
            <div class="modal-footer">
                <button class="btn btn-danger" aria-hidden="true" data-dismiss="modal">Ok</button>
            </div>
        </div>
    </div>
</div>
<form class="form-horizontal" role="form">
    <div class="form-group">
        <input type="hidden" id="id" name="quiniela.id" value="${quiniela.id }"/>
    </div>
    <div class="form-group">
        <label for="plantillaOwnerQuiniela" class="col-sm-4 control-label">Quiniela</label>
        <div class="col-sm-5">
            <select class="form-control" name="quiniela.plantillaOwnerQuiniela" id="plantillaOwnerQuiniela"  ${read }>
                <c:forEach items="${lista2 }" var="pl">
                    <option value="${pl.id }"
                    <c:if test="${pl.id == quiniela.plantillaOwnerQuiniela.id}">selected="selected"</c:if>>${pl.nombrePlantillaOwner} - ${pl.precioQuiniela}Bs</option>
                </c:forEach>
            </select>
        </div>
    </div>
</form>
$("#myModal").modal("show");
我读到可能元素不在Dom中,然后我尝试使用这个函数来查看是否发生了一些事情,但它不起作用

$('#plantillaOwnerQuiniela').on('change',function() {
    var selectVal = $('#plantillaOwnerQuiniela :selected').val();
    alert(selectVal);
});

只是一个猜测,但在绑定
更改
侦听器时,
plantillaOwnerQuiniela
元素似乎不在DOM中

请尝试以下操作:

$("body").on("change", "#plantillaOwnerQuiniela", function(e){
    var selectVal = $(e.currentTarget).val();
    alert(selectVal);
});

使用
.on()
将侦听器附加到
主体
并将其委托给选择,而不是附加到选择本身;pageload上可能存在也可能不存在。您应该将更改事件绑定到popover initialize方法,如下所示:

$('.button').popover({ 
    html : true,
    content: function() {
        return $('#popover_content_wrapper').html();
    }                 
}).parent().delegate('#plantillaOwnerQuiniela', 'change', function() {
    alert($(this).val());
});

这就是例子

在尝试读取if的值时,我通过包含modal body类修复了自己的问题。像这样:

$('.modal-body #plantillaOwnerQuiniela :selected').val();

我认为你是对的,因为select开始是空的,然后我按下create按钮,它将信息加载到表单中,然后显示模式。但这不起作用我用的是Bootstrap3模式。。我真的想不出如何用我的代码来尝试你的答案,我用不同的方式显示我的模式它不起作用,因为我也在使用struts2。。当我显示模式时,我正在填充选择框数据调用一个操作