JS简单函数调用不起作用


JS simple function call is not working

im Codeginiter 及其开发的新内容。 当我在那里按下选项时,我使用了保管箱(HTML 选择框)。 我需要触发警报 box.in 我使用以下代码执行此操作。但它对我不起作用。我可以找到我犯的错误,请帮助我。

<html lang="en">
    <head>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <title>Ask Questions</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">

        <link href="<?php echo base_url(); ?>css/bootstrap.min.css" rel="stylesheet">
        <link href="<?php echo base_url(); ?>css/style.css" rel="stylesheet"> 
<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.min.js">
                   // untill select catogory tags are disabled

                   function activate_match()
                   {
                      // var tnmt_id = "hip hure";
                       alert("tested");
                       //Get the id of the tournament selected in the list
                   }

</script>

    </head>
<body>
        <div class="container">
            <p class="help-block">
                How to Ask </br>
                Is your question about DIY ? </br>
                We prefer questions that can be answered, not just discussed.<br>
                Provide details. Share your experience. </br>
            </p>

            <div class="form-group">
                <?php
                echo form_open('homepage/askquestionview');
                echo validation_errors();
                ?>  
            </div>
            <p> </p>

            <div class="form-group">

                <select name="cat" id="cat" onchange="activate_match()">
<?php
                   foreach($catogories as $cat) {
                    echo'<option value="' . $cat . '">' . $cat . '</option>';
                }
                ?>
                </select>


            </div>
            <div class="form-group">
                <p>
                    Description: <br/>
                    <textarea name="decription" rows="5" cols="100"> </textarea>
                </p>
            </div>

            <p></p>                
            <div class="form-group">
                Declare new Tags:<br/>
                <input type="text" value="" name="tag">
                </p>
            </div>
        </div>                             <p>
            <input type="submit"  class="btn btn-success btn-block" value="Post Your Question" id="postQuestion">
        </p>
        <?php echo form_close(); ?>

    </body>

</html>
您需要

有两个单独的script标签:

<script src="<?php echo base_url(); ?>js/jquery.min.js"></script>
<script>
   function activate_match() {
       // var tnmt_id = "hip hure";
       alert("tested");
       //Get the id of the tournament selected in the list
   }
</script>

如果script具有src属性,则忽略其内部内容。所以第一个脚本是加载jQuery,第二个脚本是声明函数。

你不应该使用内联JavaScript。由于你有jQuery可用,这应该可以解决问题:

$(document).ready(function(){
    $('#cat').change(function(){
        alert('Tested! Current select value is: ' + $(this).val());
    });
});

此外,需要为自定义代码使用单独的脚本标签,并单独使用包含文件 - 您已将函数嵌入到 jQuerys 脚本标签中,这就是它不起作用的原因。

html 中的更改

<select name="cat" id="cat" onchange="activate_match(this.value)">
<?php
                   foreach($catogories as $cat) {
                    echo'<option value="' . $cat . '">' . $cat . '</option>';
                }
                ?>
 </select>

在您的脚本中

<script type="text/javascript" src="<?php echo base_url(); ?>js/jquery.min.js"></script>
<script>
                   // untill select catogory tags are disabled

                   function activate_match(id)
                   {
                      // var tnmt_id = "hip hure";
                       alert(id);
                       //Get the id of the tournament selected in the list
                   }

</script>