javascript jquery选择不工作

时间:2016-09-29 02:27:14

标签: javascript jquery html jquery-chosen

<!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无效,我的代码出了什么问题? 我的选择标记只显示为默认表单....

P.S。 Jquery代码位于另一个文件中(&#34; ff.js&#34;)

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

image

3 个答案:

答案 0 :(得分:1)

您需要像这样更改您的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);
    }
});

答案 1 :(得分:0)

如何使用更改事件?

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

答案 2 :(得分:0)

我不太明白你的问题,但试试这个 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 ;-)");
    }
});