JS简单的函数调用不起作用

时间:2014-12-26 09:53:42

标签: javascript php jquery html codeigniter

我对codeginiter及其开发的新手。当我按下选项时,我使用了dropbox(html选择框)。我需要触发一个警告框。为了做到这一点我用下面的代码。但它对我不起作用。我可以找到我所做的错误,请帮助我。

<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>

3 个答案:

答案 0 :(得分:3)

您需要有两个单独的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,第二个脚本是声明函数。

答案 1 :(得分:0)

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

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

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

答案 2 :(得分:0)

更改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>
相关问题