开发者

Hiddenfield value not updating - Jquery

开发者 https://www.devze.com 2023-04-11 17:57 出处:网络
I have a problem with Jquery : <input type=\"hidden\" id=\"is_subpage\" value=\"FALSE\" /> <input type=\"hidden\" id=\"is_page\"value=\"TRUE\"/>

I have a problem with Jquery :

 <input type="hidden" id="is_subpage" value="FALSE" />
<input type="hidden" id="is_page"  value="TRUE"/>
<input type="hidden" id="page_id" value="0"/>
<input type="hidden" id="page_domain" value="{tic}" />
<input type="hidden" id="sub_page_id" value="0"/>
<input type="submit" value="Save"/>

The above one is a portion of Html View code ,where page_id is making me trouble. On the initialization its value is 0.

$.post("{t_url}", { selector : "load-content",id :dom },
   function(data) {

   $('#is_page').val('true');
   $('#is_subpage').val('false');
   $('$page_domain').val($('#page-list').text());
   $('#editor').val(''); 
   $('#page_id').val(data.user_template_id); // - Here is the problem

   },'json'); 

The above code executing when i change the lis开发者_JAVA技巧tBox,i can alert data.user_template_id,on that time i am getting values correct.But i can't apply that on to Hidden field page_id.

what is wrong with me ? the main thing is it worked well about 2 hours ago ,but i made massive changes to system nw ,but why i cant to set the value ???

Thanks in advance.


It's because in jQuery ajax calls are asynchronous. You are trying to alert the value of page_id when in fact it is not yet set. If you alert it exactly the line after you set it, you'll see that it's being set properly.

If you want to execute some code somewhere out of the callback function, you can, for example bind a custom event to it to check if the data has been set, for example:

$("#page_id").bind("valueSet", function () {
  alert($(this).val());
}

And in your callback function, right after you set the page_id value:

$("#page_id").trigger("valueSet");


I solved this,just replaced first line.

$.post("{t_url}", { selector : "load-content",id :dom },
   function(data) {
   $('#page_id').val(data.user_template_id); // - Just added this line of code FIRST
   $('#is_page').val('true');
   $('#is_subpage').val('false');
   $('$page_domain').val($('#page-list').text());
   $('#editor').val(''); 
  // $('#page_id').val(data.user_template_id); // - REmoved from here

   },'json'); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号