2014-01-16 33 views
0

我想在頁面加載時啓用btn1並禁用btn2。當我在txtbox1中輸入值時,然後,在單擊btn1時,應禁用btn1並啓用btn2。如果我點擊btn2,那麼btn2也應該被禁用。爲什麼不同的瀏覽器在「onclick」後效果不一樣

$value= $_POST['tb1'.$id]; 
echo '<form name="f1" method="POST" action="">'; 
echo '<input type="text" id="txtbox1'.$id.'" name="tb1'.$id.'" value="'.$value.'" />'; 
echo '<input type="submit" id="btn1'.$id.'" name = "btnstart'.$id.'" value="START" onClick="document.getElementById(\'btn2'.$id.'\').disabled=false;this.disabled=true;"/><input type="submit" id="btn2'.$id.'" name = "btnend'.$id.'" value="End" onClick="this.disabled=true;" disabled/>'; 
echo '</form>'; 

if (isset($_POST['tb1'.$id])) { 
echo $value; 
} 

當我使用Chrome瀏覽器時,我可以執行上述按鈕效果,但無法獲取txtbox1值。 當我使用IE和Firefox時,我可以得到txtbox1的值,但按鈕效果不是我想要的。這意味着,我在txtbox1中輸入值,然後單擊btn1,然後它會自動啓用btn2,然後禁用兩者,然後恢復到原始狀態(btn1 enable,btn2 disable)。

如何解決這個問題呢?

+2

你的代碼中有一個錯字,第5行。 – Daedalus

+0

修正了錯字錯誤Thankyou – user3118482

+0

@Daedalus你能幫助我嗎? – user3118482

回答

1

既然你有動態按鈕名,其最好不要碰他們確定停用/啓用的功能,你想:

使用jQuery:

$(document).ready(function() { 

/*Operations on the 1st button click */ 
    $('#form').children("input[type='submit']:first").click(function() { 
     $(this).attr("disabled", "disabled"); /*disable the 1st button */ 
     $('#form').children("input[type='submit']").eq(1).removeAttr("disabled"); /*enable the 2nd button */ 
    }); 

/*Operations on the 2nd button click */ 
    $('#form').children("input[type='submit']").eq(1).click(function() { 
     $(this).attr("disabled", "disabled"); 

    }); 
}); 

此外:給您形式的ID,是這樣的:

echo '<form name="f1" id="form" method="POST" action="">'; 
/* check the form id tag ^^ here*/ 

basic demo here 但您將無法看到它完整的功能!

+0

所以我沒有需要有onclick嗎?只是''? – user3118482

+0

yes .. .remove all inline javascripts !! – NoobEditor

+0

@ user3118482:另外,請務必在您的代碼頂部添加jquery文件,您的html的頭部 – NoobEditor

0

從來不寫你這樣的代碼,這使得像疼痛調試,只是做一個script標籤和處理一切有這樣的,點這裏大約是setAttributeremoveAttribute

<?php 
// your server side code 
// finish declaring your variables like 
$input_id = 'an_id'; 
$input_value = 'some_value'; 
?> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>testing</title> 

    </head> 
    <body> 
     <form name="f1" method="POST"> 
      <input type="text" id="txtbox1<?php echo $input_id; ?>" name="tb1<?php echo $input_id; ?>" value="<?php echo $input_value; ?>" /> 
      <input type="button" id="btn1<?php echo $input_id; ?>" name="btnstart<?php echo $input_id; ?>" value="START" /> 
      <input type="button" id="btn2<?php echo $input_id; ?>" name="btnend<?php echo $input_id; ?>" value="End" disabled="disabled"/> 
     </form> 
     <script type="text/javascript"> 
      btn1 = document.getElementById('btn1<?php echo $input_id; ?>'); 
      btn2 = document.getElementById('btn2<?php echo $input_id; ?>'); 
      btn1.onclick = function() { 
       console.log(this); 
       btn1.setAttribute("disabled","disabled"); 
       btn2.removeAttribute("disabled"); 
       return false; 
      }; 
     </script> 
    </body> 
</html> 
相關問題