javascript - html button stay in disabled status after a form submit -


in below code. there total 8 textboxes wrapped divs, ids fa1 fa8.

  1. fa1 , fa2 set display default

  2. using add \ remove buttons(addfa , removefa) add , remove other divs , using hidden input element track count using value(default value 3)

  3. add button disabled once show hidden div (that @ countfa = 9) , remove buton enabled once there 3 or more textboxes (ie countfa value 4 or more )

  4. fa3 fa8 set display:none , using above mentioned add \ remove ids display them

  5. i need keep textbox along data entered user after form submit.

issue facing: using php code hidden input element update value depednds upon user's div id selection after form submit. see eventhough there 3 or more textboxes after form submit, remove button keep in disabled state. have checked whether hidden input value updating new value , see getting updated, still remove button disabled state. idea why not showing in enabled state.

my html code:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html> <body> <form  id="main" name="main" action="#text" method="post" >   <div id="fa1"> <input  class="textbox" id="tbox1" name="tbox1" type="text" value="<?php if(isset($_post['tbox1'])) { echo htmlentities ($_post['tbox1']); }?>" /></span> </div>  <div id="fa2"> <input  class="textbox" id="tbox2" name="tbox2" type="text" value="<?php if(isset($_post['tbox2'])) { echo htmlentities ($_post['tbox2']); }?>" /></span> </div>  <div id="fa3" style="<?php if(empty($_post['tbox3'])) { echo "display:none;"; } else { echo "display:block;"; } ?>"> <input  class="textbox" id="tbox3" name="tbox3" type="text" value="<?php if(isset($_post['tbox3'])) { echo htmlentities ($_post['tbox3']); }?>" /></span> </div>  <div id="fa4" style="<?php if(empty($_post['tbox4'])) { echo "display:none;"; } else { echo "display:block;"; } ?>"> <input  class="textbox" id="tbox4" name="tbox4" type="text" value="<?php if(isset($_post['tbox4'])) { echo htmlentities ($_post['tbox4']); }?>" /></span> </div>  <div id="fa5" style="<?php if(empty($_post['tbox5'])) { echo "display:none;"; } else { echo "display:block;"; } ?>"> <input  class="textbox" id="tbox5" name="tbox5" type="text" value="<?php if(isset($_post['tbox5'])) { echo htmlentities ($_post['tbox5']); }?>" /></span> </div>  <div id="fa6" style="<?php if(empty($_post['tbox6'])) { echo "display:none;"; } else { echo "display:block;"; } ?>"> <input  class="textbox" id="tbox6" name="tbox6" type="text" value="<?php if(isset($_post['tbox6'])) { echo htmlentities ($_post['tbox6']); }?>" /></span> </div>  <div id="fa7" style="<?php if(empty($_post['tbox7'])) { echo "display:none;"; } else { echo "display:block;"; } ?>"> <input  class="textbox" id="tbox7" name="tbox7" type="text" value="<?php if(isset($_post['tbox7'])) { echo htmlentities ($_post['tbox7']); }?>" /></span> </div>  <div id="fa8" style="<?php if(empty($_post['tbox8'])) { echo "display:none;"; } else { echo "display:block;"; } ?>"> <input  class="textbox" id="tbox8" name="tbox8" type="text" value="<?php if(isset($_post['tbox8'])) { echo htmlentities ($_post['tbox8']); }?>" /></span> </div>  <?php  if(isset($_post['countfa'])){    $valueid = $_post['countfa'];  ?> <input  type="hidden" id="countfa" name="countfa" value="<?= $valueid ?>" readonly> <?php  }else{   ?>  <input  type="hidden" id="countfa" name="countfa" value="3" readonly>   <?php  } ?>  <button type="button" onclick="addnewfa();" id="addfa" > + add new fa </button> <button  type="button" onclick="removenewfa();" id="removefa" disabled="disabled"> - remove fa</button>   <input  id="generate" type="submit"  name="script" value="create symcli script" />  </form>  </body> </html> 

and javascript code:

function addnewfa()  { var facount = parseint($('#countfa').val(),9) ; document.getelementbyid("test1").innerhtml = facount;     if( facount < 10) {         facount = facount+1;         document.getelementbyid("test2").innerhtml = facount;             for(i=3;i<10;i++)                 {                     if( i<facount )                         $('#fa'+i).slidedown("fast");                     else                         $('#fa'+i).slideup("fast");                  }     $('#countfa').val(facount);   }     if( facount >=9 )     { $('#addfa').attr('disabled','disabled');}      if( facount >=4 )     { $('#removefa').removeattr("disabled");} }    function removenewfa()  { var facount = parseint($('#countfa').val(),10) ; document.getelementbyid("test3").innerhtml = facount;     if( facount >3) {         facount = facount-1;         document.getelementbyid("test4").innerhtml = facount;             for(i=3;i<10;i++)                 {                     if( i<facount )                         $('#fa'+i).slidedown("fast");                     else                         $('#fa'+i).slideup("fast");                                  }                 $('#countfa').val(facount);              }                 if( facount <=3 )             { $('#removefa').attr('disabled','disabled');}          if( facount <=8 )             { $('#addfa').removeattr("disabled");       }        }    

i have setup php fiddle note: in php fiddle add button not functioning after form submit. see on actual site working fine. remove button still not working

i believe reason buttons displaying because code have enable/disable them placed within click handlers of each of items.

you should move following code :

if( facount >=9 ) { $('#addfa').attr('disabled','disabled');}  if( facount >=4 ) { $('#removefa').removeattr("disabled");} 

into seperate function, i.e.

function checkbuttons() {     var facount = parseint($('#countfa').val(),9) ;     if( facount >=9 )     { $('#addfa').attr('disabled','disabled');}      if( facount >=4 )     { $('#removefa').removeattr("disabled");}     if( facount <=3 )     { $('#removefa').attr('disabled','disabled');}      if( facount <=8 )     { $('#addfa').removeattr("disabled");}  } 

and call function after submit of form has run. should call function click handlers (to minimise copied code)

$(document).ready(function() {     checkbuttons(); }); 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -