Problem with the onchange event of select box in cakePHP
I have wriiten a below code in my view page for automatically populate select box with the first and last names of students from the session and to fill automatically the name text box with the value of the option selected from the frop down list . but the autofilling of name is not happening on selecting the option from the list.please help me out to solve this problem.
$javascript->link('jquery-1.5.1.min.js');
$javascript->link(array('jquery', 'form'));
$max_students = $this->Session->read('student_count');
$student_name = array();
$student_name[] = "-- Select --";
for($i=0;$i<$max_students;$i++)
{
$student_first_name 开发者_开发问答 = $this->Session->read('Student.'.$i.'.FirstName');
$student_last_name =$this->Session->read('Student.'.$i.'.LastName');
$student_name[] = ($student_first_name." ".$student_last_name);
}
$selectDomId = $this->Form->domId('students');
$firstnameDomId = $this->Form->domId('Name');
$this->Html->scriptBlock("
jQuery(function($){
$('#{$selectDomId}').change(function(event){
$('#{$firstnameDomId}').val( $(this).val() );
});
});
",array('inline'=>false));
echo $this->Form->create('Contact',array('controller'=>'contacts','action'=>'addcontact'));
echo "<strong>Same As</strong>";
echo $this->Form->select('students',$student_name,$this->Form->domId(array(),'students'));
echo $this->Form->input('Name');
I don't really have much of an answer for you - but are you able to use FireBug or some kind of equivalent developer tool to find out if there are any JavaScript errors being thrown when you change the value of the select
control? You should also take a look at the generated HTML source for the page to ensure that the script code was generated correctly.
Try inserting a debugging statement inside your JavaScript function to make sure the call happens too:
$this->Html->scriptBlock("
jQuery(function($){
$('#{$selectDomId}').change(function(event){
$('#{$firstnameDomId}').val( $(this).val() );
});
alert('Test');
});
",array('inline'=>false));
精彩评论