Reputation: 43
I don't know how to enable and disable a text field using select box option.
Maybe I wrote this totally wrong in the code below. If I did, please tell me correct code.
<body>
<table width="500" border="1">
<form style="text-align:center" id="form1" name="form1" method="post" action="">
<tr>
<th width="32" nowrap="nowrap">Status</th>
<th width="32" nowrap="nowrap">Runs</th>
<th width="16" nowrap="nowrap">6</th>
</tr>
<?php
$('#Status1').change(function(){
var theVal = $('#Status1').val();
switch(theVal){
case'0':
$('#Runs1').prop('disabled', true);
break;
case'1':
$('#Runs1').prop('disabled', false);
break;
case'2':
$('#Runs1').prop('disabled', true);
break;
}
});
?>
<tr>
<td align='center'>
<select id='Status1'><option value='0'>Not Playing</option><option value='1'>Playing</option><option value='2'>Out</option></select></td>
<td align='center'>
<input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='Runs1' type='text' id='Runs1' size='5' maxlength='5' value='' />
</td>
</tr>
</form>
</table>
</body>
Upvotes: 1
Views: 2514
Reputation: 43
friends this is solution of my question with the help of you people thanks.
<html>
<head>
<script type="text/javascript" src="../jquery.min.js"></script>
<SCRIPT LANGUAGE="JavaScript">
$(document).ready(function(){
$('#Status1').change(function(){
var theVal = $('#Status1').val();
switch(theVal){
case'0':
$('#Runs1').prop('disabled', true);
break;
case'1':
$('#Runs1').prop('disabled', false);
break;
case'2':
$('#Runs1').prop('disabled', true);
break;
}
});
});
</script>
</head>
<body>
<table width="500" border="1">
<form style="text-align:center" id="form1" name="form1" method="post" action="">
<tr>
<th width="32" nowrap="nowrap">Status</th>
<th width="32" nowrap="nowrap">Runs</th>
<th width="16" nowrap="nowrap">6</th>
</tr>
<tr>
<td align='center'>
<select id='Status1'><option value='0'>Not Playing</option><option value='1'>Playing</option><option value='2'>Out</option></select></td>
<td align='center'>
<input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='Runs1' type='text' id='Runs1' size='5' maxlength='5' value='' />
</td>
</tr>
</form>
</table>
</body>
</html>
Upvotes: 0
Reputation: 40639
Try it like:
HTML
<body>
<table width="500" border="1">
<form style="text-align:center" id="form1" name="form1" method="post" action="">
<tr>
<th width="32" nowrap="nowrap">Status</th>
<th width="32" nowrap="nowrap">Runs</th>
<th width="16" nowrap="nowrap">6</th>
</tr>
<tr>
<td align='center'>
<select id='Status1'><option value='0'>Not Playing</option><option value='1'>Playing</option><option value='2'>Out</option></select></td>
<td align='center'>
<input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='Runs1' type='text' id='Runs1' size='5' maxlength='5' value='' />
</td>
</tr>
</form>
</table>
</body>
SCRIPT
<script type="text/javascript">
$(document).ready(function(){
$('#Status1').change(function(){
var theVal = $('#Status1').val();
switch(theVal){
case'0':
$('#Runs1').prop('disabled', true);
break;
case'1':
$('#Runs1').prop('disabled', false);
break;
case'2':
$('#Runs1').prop('disabled', true);
break;
}
});
});
</script>
But first check you have added a jquery library file
in the head section
.
Upvotes: 1
Reputation: 1107
You have not included the jquery file and used tags instead of for the javascript code
<body>
<table width="500" border="1">
<form style="text-align:center" id="form1" name="form1" method="post" action="">
<tr>
<th width="32" nowrap="nowrap">Status</th>
<th width="32" nowrap="nowrap">Runs</th>
<th width="16" nowrap="nowrap">6</th>
</tr>
<script src="../jquery.js"></script>
<script type="text/javascript">
$('#Status1').change(function(){
var theVal = $('#Status1').val();
switch(theVal){
case'0':
$('#Runs1').prop('disabled', true);
break;
case'1':
$('#Runs1').prop('disabled', false);
break;
case'2':
$('#Runs1').prop('disabled', true);
break;
}
});
</script>
<tr>
<td align='center'>
<select id='Status1'><option value='0'>Not Playing</option><option value='1'>Playing</option><option value='2'>Out</option></select></td>
<td align='center'>
<input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='Runs1' type='text' id='Runs1' size='5' maxlength='5' value='' />
</td>
</tr>
</form>
</table>
</body>
Upvotes: 1
Reputation: 36531
load jquery in your <head>
and add the javascript there.. not inside <?php
(php code)
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$('#Status1').change(function(){
var theVal = $('#Status1').val();
switch(theVal){
case'0':
$('#Runs1').prop('disabled', true);
break;
case'1':
$('#Runs1').prop('disabled', false);
break;
case'2':
$('#Runs1').prop('disabled', true);
break;
}
});
</script>
<body>
....//rest of your code
Upvotes: 1
Reputation: 1031
The code for enabling/disabling the #Run1 input box is nested between PHP tags (server side), but this code is written in javascipt (client-side).
<body>
<table width="500" border="1">
<form style="text-align:center" id="form1" name="form1" method="post" action="">
<tr>
<th width="32" nowrap="nowrap">Status</th>
<th width="32" nowrap="nowrap">Runs</th>
<th width="16" nowrap="nowrap">6</th>
</tr>
<script type="text/javascript">
$('#Status1').change(function(){
var theVal = $('#Status1').val();
switch(theVal){
case'0':
$('#Runs1').prop('disabled', true);
break;
case'1':
$('#Runs1').prop('disabled', false);
break;
case'2':
$('#Runs1').prop('disabled', true);
break;
}
});
</script>
<tr>
<td align='center'>
<select id='Status1'><option value='0'>Not Playing</option><option value='1'>Playing</option><option value='2'>Out</option></select></td>
<td align='center'>
<input style='font-weight:bold; background:#FFFFFF; text-align:right; color:#000000;' disabled='disabled' name='Runs1' type='text' id='Runs1' size='5' maxlength='5' value='' />
</td>
</tr>
</form>
</table>
Upvotes: 2
Reputation: 6000
Replace <?php
with <script>
and ?>
with </script>.
Rest all the thing is fine.
The code you wrote in php tags is a Jquery code, so it should be under script tags.
Upvotes: 1