2013-06-29 109 views
0

我有我的頁面上的表單,當我點擊一個按鈕的形式隱藏和另一種形式顯示。在這裏,我打電話jQuery的show()hide()按鈕和更改按鈕的valueid屬性。然後使用新的按鈕id屬性我使用click()函數就可以了。但不幸的是它不起作用。輸入點擊不工作

這是fiddle

HTML:

<form id="sign_in"> 
    <table style="margin: 0 auto;"> 
     <tr> 
      <td align="center"> <span style="font-weight: bold;font-size: 2em;">Sign In</span> 

      </td> 
      <tr> 
       <td> 
        <input class="input" type="email" placeholder="Username" name="username" /> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input class="input" type="password" placeholder="Password" name="password" /> 
        <br /> 
       </td> 
      </tr> 
      <tr align="right"> 
       <td> 
        <input class="btn" type="submit" value="Sign In"> 
       </td> 
      </tr> 
    </table> 
</form> 
<form id="sign_up"> 
    <table style="margin: 0 auto;"> 
     <tr> 
      <td align="center"> <span style="font-weight: bold;font-size: 2em;">Sign Up</span> 

      </td> 
      <tr> 
       <td> 
        <input class="input" type="email" placeholder="Username" name="username" /> 
       </td> 
      </tr> 
      <tr> 
       <td> 
        <input class="input" type="password" placeholder="Password" name="password" /> 
        <br /> 
       </td> 
      </tr> 
      <tr align="right"> 
       <td> 
        <input class="btn" type="submit" value="Sign In"> 
       </td> 
      </tr> 
    </table> 
</form> 
<br/> 
<input id="sign_up_btn" class="btn" type="submit" style=" font-weight:bold; height:40px; width: 292px;" value="Create An Account"></input> 

JS:

$(document).ready(function() { 
    $("#sign_up_btn").click(function() { 
     $("#sign_in").hide("slow"); 
     $("#sign_up").show("slow"); 
     $(this).attr("value", "Already have an account?"); 
     $(this).attr("id", "sign_in_btn"); 
    }); 
    $("#sign_in_btn").click(function (e) { 
     e.stopPropagation(); 
     $("#sign_up").hide("slow"); 
     $("#sign_in").show("slow"); 
     $(this).attr("value", "Create An Account"); 
     $(this).attr("id", "sign_up_btn"); 
    }); 
}); 
+0

更改ID也絕對沒有什麼,該事件已經被綁定到該元素。 – adeneo

回答

2

您需要使用事件代表團#sign_in_btn當您綁定單擊事件

$(document.body).on('click',"#sign_in_btn",function (e) { 
     e.stopPropagation(); 
     $("#sign_up").hide("slow"); 
     $("#sign_in").show("slow"); 
     $(this).attr("value", "Create An Account"); 
     $(this).attr("id", "sign_up_btn"); 
}); 
不存在
1

使用toggle()

$(document).ready(function() { 
    $("#sign_up_btn").click(function() { 
     $("#sign_in, #sign_up").toggle("slow"); 
     $(this).val(function(_, val) { 
      return val == 'Create An Account' ? 'Already have an account?' : 'Create An Account'; 
     }); 
    }); 
}); 

FIDDLE

0
$(document).ready(function() { 
    $(document).on('click', "#sign_up_btn",function() { 
     $("#sign_in").hide("slow"); 
     $("#sign_up").show("slow"); 
     $(this).attr("value", "Already have an account?"); 
     $(this).attr("id", "sign_in_btn"); 
    }); 
    $(document).on('click', "#sign_in_btn", function (e) { 
     e.stopPropagation(); 
     $("#sign_up").hide("slow"); 
     $("#sign_in").show("slow"); 
     $(this).attr("value", "Create An Account"); 
     $(this).attr("id", "sign_up_btn"); 
    }); 
});