admin管理员组

文章数量:1435859

I am new to code igniter and I have searched the following question in number of StackOverflow 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=".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

I am new to code igniter and I have searched the following question in number of StackOverflow 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./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

Share Improve this question edited Jul 28, 2016 at 6:19 Hardik Vaghani 2,17327 silver badges47 bronze badges asked Jul 28, 2016 at 6:06 NaNNaN 1,0723 gold badges13 silver badges27 bronze badges 1
  • See browser console if any error is showing – Vinie Commented Jul 28, 2016 at 6:30
Add a ment  | 

4 Answers 4

Reset to default 5

You are using <select> id as category

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

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

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

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.

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

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>

There is mistake with your jquery selector.

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

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

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

     //ur code here

});

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.

Change this following:-

<select name="select1" id="category">
    <option value="0">None</option>
    <option value="1">First</option>
</select>
<scriptsrc="http://ajax.googleapis./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) {

            }
        });
    });
});

本文标签: javascriptJQuery on dropdown list change is not working in codeigniterStack Overflow