JavaScript and client side scripting.
Moderator: General Moderators
Lphp
Forum Commoner
Posts: 74 Joined: Sun Jun 26, 2011 9:56 pm
Post
by Lphp » Thu Mar 26, 2015 10:56 am
I have a table , the rows are select from db, the rows is dynamic , and I want my fist td''s value control my second td's value . I have the following code, how can I let my script know which row I click?
Code: Select all
<tr id =1><td><select id="exam_type">
<option>--Select--</option>
<option>exam 1</option>
<option>exam2</option>
<option>exam3</option>
</select>
<label>Select Grade:</label>
<select id="grade">
<!-- Dependent Select option field -->
</select>
</div>
<tr id =2><td><select id="exam_type">
<option>--Select--</option>
<option>exam 3</option>
<option>exam2</option>
<option>exam4</option>
</select>
<label>Select Grade:</label>
<select id="grade">
<!-- Dependent Select option field -->
</select>
</div>
</td></tr>
more tr
$("#id" +?).change(function(){
// Do something here.
}
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Thu Mar 26, 2015 12:08 pm
IDs are unique. Classes can apply to many elements, maybe:
Code: Select all
<tr id =1><td><select id="exam_type1" class="exam_type">
<option>--Select--</option>
<option>exam 1</option>
<option>exam2</option>
<option>exam3</option>
</select>
<label>Select Grade:</label>
<select id="grade">
<!-- Dependent Select option field -->
</select>
</div>
<tr id =2><td><select id="exam_type2" class="exam_type">
<option>--Select--</option>
<option>exam 3</option>
<option>exam2</option>
<option>exam4</option>
</select>
<label>Select Grade:</label>
<select id="grade">
<!-- Dependent Select option field -->
</select>
</div>
</td></tr>
more tr
$(".exam_type").change(function(){
// get ID of changed select
}
[/quote]
You could also register two event functions, one for each ID.
(#10850)
Lphp
Forum Commoner
Posts: 74 Joined: Sun Jun 26, 2011 9:56 pm
Post
by Lphp » Thu Mar 26, 2015 10:13 pm
Thanks for the reply,
but the hard code can get the value
<<this can get the value
Code: Select all
<script language="javascript">
$(document).ready(function(){
$('#exam_type1').change(function() {
var exam_type=$("#exam_type1").val();
alert (exam_type +"Hi");
});
});
</script>
But the suggestion is not work, did I miss anything
Code: Select all
$(document).ready(function(){
$(".exam_type").change(function() {
alert(this.id);
var exam_type=$(".exam_type").val();
alert (exam_type +"Hi");
});
});
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Thu Mar 26, 2015 11:27 pm
Lphp wrote: But the suggestion is not work, did I miss anything
Code: Select all
$(document).ready(function(){
$(".exam_type").change(function() {
alert(this.id);
var exam_type=$(".exam_type").val();
alert (exam_type +"Hi");
});
});
You got the ID, but you did not use it to get the value!
Code: Select all
$(".exam_type").change(function(){
// get ID of changed select
var exam_type = $("#"+this.id).val();
alert ("exam_type=" + exam_type);
});
(#10850)