0

I have a text box and dropdown. Text box contains the province name and the dropdown contains the the cities. I want to chnage the province of the text box and dynamically change the dropdown related to the text in the textbox. I am using codeigniter framework. My view is as follows and I get the data from the database. I think there is a problem with the onchange or that kind of event of the text box (this code worked for change event for another dropdown. But it is not working for the taxt box- so the other logic is ok but I cant figure out the event of the textbox. What is the right event for the text box ? My code

<script> .........................<script>

    $(document).ready(function(){       
        $('#provincial').on('input',function()
        { 

            var id_provincia = $('#provincial').val();
                $.ajax(
                {
                    type: "POST",
                    dataType: "json", // I ADDED THIS LINE
                    url:"tenda/get_comuni/"+id_provincia,
                    success: function(comuni)
                    {
                        $('#comuni').empty();
                        $.each(comuni,function(id_comune, nome_comune)
                        {
                            var opt = $('<option />');
                            opt.val(id_comune);
                            opt.text(nome_comune);
                            $('#comuni').append(opt);
                        });            
                    }
                });
        });
    });  
</script>
</head>
<body>
<?php $comuni['#'] = 'seleziona'; ?>
<div id="tenda">
            <label for="provincia">provincia: </label><?php echo form_dropdown('id_provincia', $provincie, '#', 'id="provincia"'); ?>
           <?php echo form_input('id_provincial','', '#', 'id="provincial"'); ?>
            <label for="comune">comune: </label><?php echo form_dropdown('id_comune', $comuni, '#', 'id="comuni"'); ?><br />
</div>

4

1 回答 1

0

For a textbox you can use keyup or onchange (fired when element loses focus) events.

$('#provincial').keyup(function(event){...});

If you want to sent POST request when you press enter button, you can handle it using this code snippet:

if (event.which == 13) {
  event.preventDefault();
  .. // DO POST Request
}
于 2013-09-15T05:08:26.057 回答