为什么我的 OnSelect 事件没有相应地触发

即使是 php 格式,也无法在不重新加载整个页面的情况下运行我的选择事件。


    <html>

    <head>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link rel ="stylesheet" href = "style.css">

        <script src="https://code.jquery.com/jquery-3.4.1.js"></script>     

        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

        <script src='https://kit.fontawesome.com/a076d05399.js'></script>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>


        <script src = "home.js"></script>

        <title>Javascript on Steroids</title>

    </head>

    <body> 



            <select id = "purchaseId"  >

                <option value="1" selected >1</option>

                <option value="2">2</option>

            </select>


    </body>

    </html>

您好,即使是 php 格式,也无法在不重新加载整个页面的情况下运行我的选择事件。


    $myobject = document.getElementById("purchaseType");


    $myobject.addEventListener("change",function(){

      alert("tt");

    })


回首忆惘然
浏览 247回答 3
3回答

紫衣仙女

您选择的 ID 不等于您在脚本中调用的 ID。此外,您可以将代码更改为:$("#purchaseId").change(function() {&nbsp;&nbsp; alert('tt')&nbsp;});这是一个小提琴。 https://jsfiddle.net/Lnroksgz/

元芳怎么了

需要在脚本之前包含这个只能触发$(文档).ready(函数(){// 你的代码 });

湖上湖

&nbsp;use this in script&nbsp; &nbsp; &nbsp;myobject = document.getElementById("purchaseId");&nbsp; &nbsp; &nbsp;myobject.addEventListener("change",function(){&nbsp; &nbsp; &nbsp; alert("tt");&nbsp; &nbsp; &nbsp;})
打开App,查看更多内容
随时随地看视频慕课网APP