Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script problem

Status
Not open for further replies.

derketa

Programmer
May 13, 2002
43
TR
I have a simple script runs correctly on my computer:
function checkSel(){
if(document.form1.select1.value=='selected')
{
document.form1.select2.disabled = true;
}
else
{
document.form1.select2.disabled = false;
}
}
</script>

...

<form name="form1" method="post" action="">
<select name="select1" onChange="javascript:checkSel()">
<option value="1">xzdas</option><option value="selected">xzdas</option><option value="2">xzdas</option>
</select>
<select name="select2">
<option value="1">aaa</option>
</select>
</form>


but it is not working on the remote server...
onChange event gives "undefined" error.

 
First, change this; onChange="javascript:checkSel()" to this: onChange="checkSel()". Second, understand that <select> tags are arrays with the <option> being the array elements. To evaluate them, you do something like this:

if(document.form1.select1.selectedIndex=='1')

<select name="select1" onChange="javascript:checkSel()">
<option value="1">xzdas</option> // this is 0
<option value="selected">xzdas</option> // this is 1
<option value="2">xzdas</option> // this is 2
</select>

There's always a better way. The fun is trying to find it!
 
Hi,

I have changed the code as you said.
But now i have a problem with transferring var php to jscript:
<select name="<?php echo($s); ?>" onChange="javascript:checkSel($s);">
<?php if ($row["workDef"]==3) {
?>
<option selected>Never worked</option>
<option>Worked before</option>
<option>Currently Working</option>
<?php
}
 
Could you be a little more specific about the php problem (even though this is not a PHP forum, we may be able to help out)?

Maybe you can fix your problem by changing your code to read:

Code:
<select name="<?php echo($s); ?>" onChange="checkSel()">
<?php if ($row["workDef"]==3) { ?>
<option selected>Never worked</option>
<option>Worked before</option>
<option>Currently Working</option>
<?php } ?>

It's all in the details.

Jeff
 

I suggest changing your function to read:

Code:
function checkSel() {
    var myForm = document.forms['form1'];
    if(myForm.select1.options[myForm.select1.selectedIndex]=='selected')
    {
        myForm.select2.disabled = true;
    } 
    else 
    {
        myForm.select2.disabled = false;
    }
}

Hope this helps,
Dan
 
Sorry, i have changed the function to this:

function checkSel(n){
if(document.form1.n.selectedIndex=='0')
{
for (i=2;i<7;i++){
name=i + "_" + n;
document.form1.name.disabled = true;

}
}
else
{
for (i=2;i<7;i++){
name=i + "_" + n;
document.form1.name.disabled = false;
}
}
}


in the body i have


<select name="<?php echo($s); ?>" onChange="javascript:checkSel($s);">

because i have the select names like:
<select name="<?php echo("2_".$s); ?>">

and i need to send $s to changeSel() function.


 

You cannot do this:

Code:
if(document.form1.n.selectedIndex=='0')

Because selectedIndex is an int, not a string. Change '0' to 0.

Hope this helps,
Dan
 
You do not need javascript: in any onclick for any element on any browser as tviman stated (and I followed up with in a code example).

Regarding your <select> code... you have neglected to wrap the $s in some <?php ?> tags. Still, I would change the code again so that you don't need to be so specific (and it works with your function change):

Code:
<select name="<?php echo($s); ?>"   onChange="checkSel(this.name)">

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top