2014-11-14 140 views
0

WEll,从选择下拉菜单中选择选项时出现的模式

如果选择了选择下拉菜单中的某个选项,我试图显示一个模式窗口。如果不是使用select中的选项,而是使用简单的按钮,但是我需要从select中选择它,它确实有效。

屏幕虽然改变了它的不透明度,所以我想我正在做一些事情......但错了。很明显,我有我需要的bootstrap链接和脚本,打开并关闭doc和html。

有什么建议吗?

谢谢youuu

html 
------------------------------------------------------------------------ 
    <select id="selectAction" data-style="btn-info" onchange="newGraph(this)"> 
     <option value="" disabled selected>Select your option:</option> 
     <option value="create">Create a new graph</option> 
     <option value="recall">Recall a saved graph</option> 
     </select> 

     <!--Pop up window--> 

     <div data-toggle="modal" data-target="#popUp1"> //it works if I put this in a <button> instead a div, but then, I have to click to button to make the modal to work so it in not what I want. 
<div class="modal hide fade" id="popUp1" tabindex="-1" role="dialog" aria-labelledby="popUp1" aria-hidden="true"> 
    <div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button> 
     <h4>Creating a new graph:</h4> 
     </div> 
     <div class="modal-body"> 
     <p>Here some text</p> 
     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button> 
     <button type="button" class="btn btn-success">Save changes</button> 
     </div> 
    </div> 
    </div> 
</div> 
    </div> 


script 
-------------------------------------------------------------------------------- 
function newGraph(element) { 
    if($(element).val() === 'create') { 
    $('.toolsHeader').show(); 
    $('#popUp1').modal(); 
     $('#selectAction').hide(); 
    } 
    else{ 
    return false; 
    }}; 
+0

您需要将所有内容放入'$(function(){})'中,并在使用jquery时摆脱inline'onchange'事物。这就是你需要的所有东西 – Yang 2014-11-14 13:25:42

回答

0

删除此:onchange="newGraph(this)"从HTML。

补充一点:

$('#selectAction').change(function() { 
    newGraph($(this)); 
}); 

的JS。

+1

好的答案应该提供一个解释,而不仅仅是“复制粘贴”来使它工作 – Yang 2014-11-14 13:19:14

+0

而且,你可以看到'function newGraph(element)'期望'element'是一个实例'DOMElement'或选择器,而不是'$(this).val()'返回的字符串。 – Yang 2014-11-14 13:23:22

+0

@bad_boy,这是为了注意$(this).val()问题。我修正了这一点。然而,我会认为对问题的最好解释是自我记录代码。恰好为零的人应该理解这个代码的作用。 – charles 2014-11-14 16:27:35