Jump to content

[SOLVED] disabling elements onchange


Darkmatter5

Recommended Posts

Here's my Javascript

 

function disablefield(form,element) {
    if(element='att_pc') {
        document.form.att_npc.disabled=true;
        document.form.att_beast.disabled=true;
    }
    elseif(element='att_npc') {
        document.form.att_pc.disabled=true;
        document.form.att_beast.disabled=true;
    }
    elseif(element='att_beast') {
        document.form.att_pc.disabled=true;
        document.form.att_npc.disabled=true;
    }
}

 

Here's the HTML

 

<form method='get' name='simcombat' action='http://mysite/simcombat.php?ta=20'>
  <table border='0' bgcolor='#D2B48C'>
  <tr><th colspan='4'>Combat simulator</th></tr>
  <tr><th> </th><th>Player characters</th><th>Nonplayer characters</th><th>Beasts</th></tr>
  <tr>
    <td align='right'>Attacker:</td>
    <td align='center'>
      <select name='att_pc' onchange='disablefield(this.form,this.name)'>
      <option value='0'>--SELECT--</option>
      <option value='13'>a b</option>
      <option value='2'>Joe Blow</option>
      </select>
    </td>
    <td align='center'>
      <select name='att_npc'>
      <option value='0'>--SELECT--</option>
      <option value='2'>Joe Bloe</option>
      <option value='1'>John Smith</option>
      </select>
    </td>
    <td align='center'>
      <select name='att_beast'>
      <option value='0'>--SELECT--</option>
      <option value='4'>Cat</option>
      <option value='3'>Rat</option>
      </select>
    </td>
  </tr>
  <tr><th colspan='4'><input type='submit' name='submit' value='Simulate combat'></th></tr>
  <table>
</form>

 

I'm basically wanting to disable all form elements besides the element changed.  I know my usage of "this" is incorrect.  Please help!

Link to comment
https://forums.phpfreaks.com/topic/168979-solved-disabling-elements-onchange/
Share on other sites

1. Need to use double equal signs when doing comparisons, a==b> A single equal sign will assign a value.

 

2. You can't use "form" like that (unless you named your form as "form"), needs to be forms[0] for the first form or form['name']. Or you can backwards reference it from the field object as I did below :fieldObjecy.form

 

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<script type="text/javascript">

function disablefield(fieldObj)
{
    var fields = new Array('att_pc', 'att_npc', 'att_beast');

    for(var i=0; i<fields.length; i++)
    {
        fieldObj.form[fields[i]].disabled = (fieldObj.value!=0 && fieldObj.name!=fields[i]);
    }
    return;
}

</script>

</head>

<body>
<form method="get" name="simcombat" action="http://mysite/simcombat.php?ta=20">
  <table border="0" bgcolor="#D2B48C">
  <tr><th colspan="4">Combat simulator</th></tr>
  <tr><th> </th><th>Player characters</th><th>Nonplayer characters</th><th>Beasts</th></tr>
  <tr>
    <td align="right">Attacker:</td>
    <td align="center">
      <select name="att_pc" onchange="disablefield(this)">
      <option value="0">--SELECT--</option>
      <option value="13">a b</option>
      <option value="2">Joe Blow</option>
      </select>
    </td>
    <td align="center">
      <select name="att_npc" onchange="disablefield(this)">
      <option value="0">--SELECT--</option>
      <option value="2">Joe Bloe</option>
      <option value="1">John Smith</option>
      </select>
    </td>
    <td align="center">
      <select name="att_beast" onchange="disablefield(this)">
      <option value="0">--SELECT--</option>
      <option value="4">Cat</option>
      <option value="3">Rat</option>
      </select>
    </td>
  </tr>
  <tr><th colspan="4"><input type="submit" name="submit" value="Simulate combat"></th></tr>
  <table>
</form>

</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.