English 中文(简体)
Jquery 脚印在我改动时停止工作,然后重载要选取的原始内容
原标题:Jquery script stops working when I change, then reload original elements to be selected

有几个问题,标题是.html()。 下面是一部法典,将“电子邮件地址”的文字箱输入值改为“如果它们没有输入任何内容,则将“电子邮件地址”重新贴在模版上。

I m using jquery .html(> with .post( to phpmailer to Update the contained div with "Sending. >. “成功发送”等。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
     $("#emailaddressinput").focus(function(){
          if ($(this).val() ==  Enter email address ) {
               $(this).val(  );
          }
     });
     $("#emailaddressinput").blur(function(){
          if (!$.trim($(this).val())) {
               $(this).val( Enter email address );
          }
     });
});
</script>

Here s the code that POSTS to phpmailer. On success the response is true . However, when it fails, it uses dotimeout.js to delay 3 seconds and then uses jquery .html to put the original form back in place. When it does this, the above code doesn t work anymore. At the bottom is my original html.

<script src="lib/dotimeout.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function() {
     $( #emailbutton ).click(function(e) {
          e.preventDefault();
          var email = $( #emailaddressinput ).val();
          //write  sending...  or something
          $( #emailinputcontainer ).html( Sending, please wait. );
          var generated_coupon_id =  2 ;
          var dataString =  email=  + email +  &generated_coupon_id=  + generated_coupon_id;
          $.post( send.php , dataString, function(data){
               //do stuff
               if(data== true ){
                    //say "message sent!"
                    $( #emailinputcontainer ).html( Your coupon has been emailed! );
               } else {
                    //say error
                    $( #emailinputcontainer ).html( There was an error. Please try again. );
                    $.doTimeout(3000,function() {
                         $( #emailinputcontainer ).html( <input id="emailaddressinput" type="text" value="Enter email address" /><a id="emailbutton" href="#" style="margin: 0 !important;"><span class="buttontext" >Email coupon</span></a> );
                    });
               }
          });
     });
});
</script>

The original html of the form:

<div id="emailinputcontainer">
    <input id="emailaddressinput" type="text" value="Enter email address" /><a id="emailbutton" href="#" style="margin: 0 !important;"><span class="buttontext" >Email coupon</span></a>
</div>

当电子邮件失败时,我如何在本页顶上看到我的原始文字,我又使用<条码>.html(<>/条码>)重新载录原始内容。

EDIT:实际上,我进一步推敲现在的<代码>。

最佳回答

您所期待的是.live()。 当你打电话(“#emailaddressinput”)时,马克列斯公司发现在OMS的零号邮件地址,并将这一功能与这一要素联系起来。 当你用 j子取代html时,它不再是相同的因素,而是看上去的新内容。 .live()继续看到OMD进行变革,并随着新内容的产生而对其具有约束力。

http://api.jquery.com/live/rel=“nofollow”

问题回答

你提出的问题是,你重新在所谓的“电子邮件地址”中设置一个内容,指派一名活动听众,将其与活动听众一起删除,然后建立一个新的“电子邮件地址”要素,除任何事件都不受约束外,只看它。

你可以打破 Java的文字:

function bindEmailEvents() {
     $("#emailaddressinput").focus(function(){
          if ($(this).val() ==  Enter email address ) {
               $(this).val(  );
          }
     });
     $("#emailaddressinput").blur(function(){
          if (!$.trim($(this).val())) {
               $(this).val( Enter email address );
          }
     });
}

Then you would call bindEmailEvents on $(document).ready, and after $( #emailinputcontainer ).html(...) is used.

On a side note, depending on your target audience you could take advantage of HTML5 placeholder text to serve a similar function and remove the JavaScript code:

<input placeholder="Enter email address" ...>

此外,展示和隐藏你的内容,而不是增加和删除超文本的做法可能更好:

<div id="emailmessagecontainer" style="display:hidden"></div>
<div id="emailinputcontainer"></div>

就任后,请:

$("#emailmessagecontainer").show().text(message);
$("#emailinputcontainer").hide();

在错误方面,你可以做到:

$("#emailmessagecontainer").hide();
$("#emailinputcontainer").show();




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签