JQuery.UI Dialog & $().load() & JavaScript

On an event I am showing a JQuery.UI dialog. The way JQuery.UI Dialog works is it expects an existing DIV to simply transform into the dialog. I need a form in the dialog but I need it populated and shown via ajax. So here is how I go about doing that.

  1. $( "img[title=edit_syn]" ).css( "cursor""pointer" ).click(function() {  
  2.         var dialog = $("<div class='flora'></div>" ).insertAfter( '#searchTable' );  
  3.         $(dialog).load("/aw/action/synonyms/display/"  +  this .id, function() {  
  4.           $(dialog).dialog({  
  5.             resizable: false ,  
  6.             modal: true ,  
  7.             width: 400 ,  
  8.             height: 450   
  9.           });  
  10.           $('.ui-dialog-titlebar-close' ).click(function() {  
  11.             $('searchTable' ).remove(dialog);              
  12.           })  
  13.         });  
  14.       });  


Note that the following line...

  1. $(dialog).load( "/aw/action/synonyms/display/"  +  this .id, function() {...});  


...takes the DIV element I created and performs an AJAX request to get the desired HTML. That HTML looks like...

  1. <link rel= "stylesheet"  type= "text/css"  href= "${pageContext.request.contextPath}/styles/ingrid/ingrid.css" />  
  2. <script type="text/javascript"  src= "${pageContext.request.contextPath}/scripts/jquery/ingrid/jquery.ingrid-0.9.2.js" ></script>  
  3. <script type="text/javascript"  src= "${pageContext.request.contextPath}/scripts/edit_synonyms.js" ></script>  
  4. <h3>Synonyms for   "${actionBean.globalMerchant.name}"  Merchant</h3>  
  5. <div id="syn_container" >  
  6.   <jsp:include page="syn_results.jsp" />  
  7. </div>  
  8.    
  9. <p>  
  10.   <div id="syn_errors" ></div>  
  11.   <input type="text"  name= "synonym.name"  id= "synonym.name" />  
  12.   <button id="add_syn_button" >Add Synonym</button>  
  13. </p>  
  14.    
  15. <input type="hidden"  name= "synonym.globalMerchantFk"  id= "synonym.globalMerchantFk"   
  16.        value="${actionBean.synonym.globalMerchantFk}" />  


This works. Note that now I have a script being loaded for this html. This code is where the problem eventually occurs. The first time I show the dialog everything works. There is a text field and a button. I enter something in the text field and hit enter or click the button and that value is submited via AJAX to the server and a table in the dialog is updated. However, subsequent times showing the dialog part of the script isn't working but no errors are being thrown.

  1. $(function() {  
  2.   $('#synonym//.name' ).keyup(function(e) {  
  3.     if  (e.keyCode ==  13 ) {  
  4.       saveSynonym();  
  5.     }  
  6.   });  
  7.   $('#add_syn_button' ).click(function() {  
  8.     saveSynonym();  
  9.   });  
  10. });  
  11.    
  12. function saveSynonym() {  
  13.   var syn = $('#synonym//.name' ).val();  
  14.   if  (syn.length ==  0  || syn ==  null ) {  
  15.     $('#syn_errors' ).css( "color""red" ).css( "font-weight""bold" ).html( "Please enter a name" );  
  16.   }else {  
  17.     $.post('/aw/action/synonyms/save/' , { 'synonym.globalMerchantFk' :$( '#synonym//.globalMerchantFk' ).val(),  'synonym.name' :$( '#synonym//.name' ).val()}, function() {  
  18.       $('#syn_container' ).load( '/aw/action/synonyms/load/'  + $( '#synonym//.globalMerchantFk' ).val());  
  19.     });  
  20.     $('#synonym//.name' ).val( "" );  
  21.     $('#syn_errors' ).html( "" );  
  22.   }  
  23. }  


The saveSynonym() function is always called. if I alert the lengh of $('#synonym//.name') it is always 1 so I know it is only finding one element. However, $('#synonym//.name').val() is coming back empty. Normally, that would trigger the error portion of the code. But the error isn't showing up either.

Just to reiterate, the first time the dialog is shown everything works as expected. Subsequent times it does not.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值