javascript jquery 选择无效

javascript jquery chosen not working

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>JavaScript</title>
</head>
<body>
<h1 id="hey">List King</h1>
<div class="Select-DB">
    <select id="DB" class="chosen-select" title="Select Database" data-placeholder="Select database you want!" style="width: 300px;">
        <option value=""></option>
        <option value="Pubchem3D"> Pubchem3D</option>
        <option value="molport"> Molport</option>
        <option value="ChemAxon"> ChemAxon</option>
        <option value="ChemSpider"> ChemSpider</option>
    </select>
</div>
<script src="jquery-1.12.4.js" type="text/javascript"></script>
<script src="chosen_v1.6.2/chosen.jquery.js" type="text/javascript"></script>
<script src="ff.js" type="text/javascript"></script>
</body>
</html>

jquery chosen 不工作,我的代码有什么问题? 我的 select 标签只是以默认形式出现....

p.s。 Jquery 代码位于另一个文件中 ("ff.js")

$(document).ready(function(){
    $(".chosen-select").chosen({no_results_text: "Sorry, We don't have such database ;-)"});
});

您需要像这样更改 javascript 文件

$('.chosen-select').change(function(){
    var aaa = $("#DB").val(); // get option value
    if(aaa === '') {
      // user select empty item
      console.log("Sorry, We don't have such database ;-)");
    }
    else{
      // user select valid item
      console.log(aaa);
    }
});

使用 on change 事件怎么样?

$('.chosen-select').change(function(){
     console.log(this.val());
});

我不太明白你的问题,但试试这个 LINK

HTML

<h1 id="hey">List King</h1>
<div class="Select-DB">
    <select id="DB" class="chosen-select" title="Select Database" data-placeholder="Select database you want!" style="width: 300px;">
        <option value=""></option>
        <option value="Pubchem3D"> Pubchem3D</option>
        <option value="molport"> Molport</option>
        <option value="ChemAxon"> ChemAxon</option>
        <option value="ChemSpider"> ChemSpider</option>
    </select>
</div>

JS

$('.chosen-select').change(function(){
    var aaa = $("#DB").val();
    if(aaa != ""){
    console.log(aaa);
    }
    else{
    console.log("Sorry, We don't have such database ;-)");
    }
});