当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Codeigniter + Jquery dependent dropdown works in one file but not in another

问题描述:

I'm using on a project a dropdown list dependent on selection of another dropdown list above. I'm using codeigniter as framework. Anyway I get a strange behavior of jQuery function I've found on internet.

This is the function:

<script>

$(document).ready(function(){

$('#id_filiale, #id_filiale_label').hide();

$('#id_azienda').change(function(){

var state_id = $('#id_azienda').val();

if (state_id != ""){

var post_url = "get_filiale_js/get_filiali/" + state_id;

$.ajax({

type: "POST",

url: post_url,

success: function(qFiliali) //we're calling the response json array 'cities'

{

$('#id_filiale').empty();

if(qFiliali.length != 0){

$('#id_filiale, #id_filiale_label').show();

for (var i=0;i<qFiliali.length;i++)

{

var id = qFiliali[i]['id'];

var cod = qFiliali[i]['codice_identificativo'];

$('#id_filiale').append('<option value='+id+'>'+cod+'</option>');

}

}

} //end success

}); //end AJAX

} else {

$('#id_filiale').empty();

$('#id_filiale, #id_filiale_label').hide();

}//end if

});

}); //end change

</script>

I wrote this script on two views, with the same div names on fields. In the first field I select one value and then the script starts to obtain the second value.

Hovewer, in one view the code works, when I want to run in the other view the second dropdown list return me undefined. After that, if I want to use another the time the first view, that previously works, now gives me an error 404.

The Php code was working without jQuery script, so I don't think error is there, maybe I'm doing something wrong on jQuery script. Suggestions?

EDIT:

First view:

<label for="id_azienda">Azienda:</label> <!-- Here I select one value -->

<select id="id_azienda" name="id_azienda">

<option value=""></option>

<?php foreach($groups as $each){ ?>

<option value="<?php echo $each['id']; ?>"><?php echo $each['denominazione']; ?></option>

<?php } ?>

</select>

<?php } ?>

<label for="id_filiale" id="id_filiale_label">Filiale:</label> <!-- Here the script retrieves me a set of values related to the selection above -->

<select id="id_filiale" name="id_filiale" class="form-control">

<option value=""></option>

</select>

<br>

<input type="submit" value="Continue">

Second view (not working):

<label for="id_azienda">Azienda:</label>

<select id="id_azienda" name="id_azienda" class="prova_azienda">

<option value="<?php echo $uIdF ?>"><?php echo $nome_azienda_edit ?></option>

<option value=""></option>

<?php foreach($groups as $each){ ?>

<option value="<?php echo $each['id']; ?>"><?php echo $each['denominazione']; ?></option>

<?php } ?>

</select><br>

<?php } ?>

<label for="id_filiale" id="id_filiale_label">Filiale:</label>

<select id="id_filiale" name="id_filiale" class="form-control">

<option value=""></option>

</select>

</label><br>

<input type="submit" value="Continue">

The only difference is that in the first view I have input form parameters like:

<label for="nome">Nome:</label>

<?php echo form_input('nome')?>

While in the second view, as it's a view for edit already set values, I have:

<label for="nome">Nome:</label>

<?php echo form_input('nome', set_value('nome', $uNome));?>

Second Edit:

In the not working view I've tried to print on the javascript console the result of the success function, it gives me error on PHP variables, like the variable $uNome that it's correctly retrieved from the database. Those variables are correctly retrieved as I'm on an edit page, so I don't really understand why for jQuery this is a problem.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: uIdF

Filename: views/register.php

Line Number: 57

网友答案:

Work with class (".example") and not with id: ("#example") when you have more tags that uses the jquery-function. E.g.:

<input onclick="this.value='';" class="datepicker" type="text" name="date" value="">

$(function() {
$( ".datepicker" ).datepicker();
});

The JQuery function should stands after the html part.

分享给朋友:
您可能感兴趣的文章:
随机阅读: