下拉列表更改中的JQuery在codeigniter中不起作用

时间:2022-05-26 11:20:53

I am new to code igniter and I have searched the following question in number of * threads but none of them seem to solve my problem.

我是代码点火器的新手,我在*线程的数量上搜索了以下问题,但它们似乎都没有解决我的问题。

I have the following view code:

我有以下视图代码:

<select name="select1" id="select1">
    <option value="0">None</option>
    <option value="1">First</option>
</select>
<scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">

// Ajax post
$(document).ready(function() {
    $("#select1").change(function(event) {
        event.preventDefault();
        var id = $(this).val();
        jQuery.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>" + "index.php/mycontroller/method",
            dataType: 'json',
            data: {id: id},
            success: function(res) {

            }
        });
    });
});
</script>

Ok when I change the drop down list selection nothing happens, which should actually call my controller method. Can you please help me resolve this problem?

好的,当我更改下拉列表选择时没有任何反应,实际应该调用我的控制器方法。你能帮我解决这个问题吗?

Thanks

4 个解决方案

#1


5  

You are using <select> id as category

您正在使用

But In jquery you are calling $("#select1").change(function(event) like this .

但是在jquery中你调用$(“#select1”)。change(function(event)就像这样。

You just need to change $("#select1").change(function(event) to

你只需要改变$(“#select1”)。change(function(event)to

$("#category").change(function(event)

#2


1  

Check in console if your script contains any errors.

如果脚本包含任何错误,请检入控制台。

if no errors with script, check your function getting called after change, by adding alert or console statement.

如果脚本没有错误,请通过添加alert或console语句检查更改后调用的函数。

if function getting called, check base_url. As per latest comment '/' missing.

如果函数被调用,请检查base_url。根据最新评论'/'缺失。

You can also try Following snippet :

您还可以尝试以下代码段:

//Add J Query Library here

<select name="select1" id="select1" onchange="funName(this);">
    <option value="0">None</option>
    <option value="1">First</option>
</select>

<script type="text/javascript">
 function funName(element)
 {
     var id = element.value;
     jQuery.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>" + "/index.php/mycontroller/method",
        dataType: 'json',
        data: {"id" : id},
        success: function(res) {
                console.log(res);
        }
    });
 }
</script>

#3


1  

There is mistake with your jquery selector.

你的jquery选择器有错误。

you are using $("#select1").change(function(){});

你正在使用$(“#select1”)。change(function(){});

you don't have any id reffered in this name in your section.

您的部分中没有以此名称引用的任何ID。

$("#category").change(function(event) { 

     //ur code here

});

#4


0  

You used id selector in the jquery code but that id is mentioned as a name attribute in html dropdown tag. So try the following code.

您在jquery代码中使用了id选择器,但该id在html dropdown标记中被称为name属性。因此,请尝试以下代码。

Change this following:-

更改以下内容: -

<select name="select1" id="category">
    <option value="0">None</option>
    <option value="1">First</option>
</select>
<scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
// Ajax post
$(document).ready(function() {
    $("#category").change(function(event) {
        event.preventDefault();
        var id = $(this).val();
        jQuery.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>" + "index.php/mycontroller/method",
            dataType: 'json',
            data: {id: id},
            success: function(res) {

            }
        });
    });
});

#1


5  

You are using <select> id as category

您正在使用

But In jquery you are calling $("#select1").change(function(event) like this .

但是在jquery中你调用$(“#select1”)。change(function(event)就像这样。

You just need to change $("#select1").change(function(event) to

你只需要改变$(“#select1”)。change(function(event)to

$("#category").change(function(event)

#2


1  

Check in console if your script contains any errors.

如果脚本包含任何错误,请检入控制台。

if no errors with script, check your function getting called after change, by adding alert or console statement.

如果脚本没有错误,请通过添加alert或console语句检查更改后调用的函数。

if function getting called, check base_url. As per latest comment '/' missing.

如果函数被调用,请检查base_url。根据最新评论'/'缺失。

You can also try Following snippet :

您还可以尝试以下代码段:

//Add J Query Library here

<select name="select1" id="select1" onchange="funName(this);">
    <option value="0">None</option>
    <option value="1">First</option>
</select>

<script type="text/javascript">
 function funName(element)
 {
     var id = element.value;
     jQuery.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>" + "/index.php/mycontroller/method",
        dataType: 'json',
        data: {"id" : id},
        success: function(res) {
                console.log(res);
        }
    });
 }
</script>

#3


1  

There is mistake with your jquery selector.

你的jquery选择器有错误。

you are using $("#select1").change(function(){});

你正在使用$(“#select1”)。change(function(){});

you don't have any id reffered in this name in your section.

您的部分中没有以此名称引用的任何ID。

$("#category").change(function(event) { 

     //ur code here

});

#4


0  

You used id selector in the jquery code but that id is mentioned as a name attribute in html dropdown tag. So try the following code.

您在jquery代码中使用了id选择器,但该id在html dropdown标记中被称为name属性。因此,请尝试以下代码。

Change this following:-

更改以下内容: -

<select name="select1" id="category">
    <option value="0">None</option>
    <option value="1">First</option>
</select>
<scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
// Ajax post
$(document).ready(function() {
    $("#category").change(function(event) {
        event.preventDefault();
        var id = $(this).val();
        jQuery.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>" + "index.php/mycontroller/method",
            dataType: 'json',
            data: {id: id},
            success: function(res) {

            }
        });
    });
});