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
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.