开发者

jQuery, wont change value but will change other attributes

开发者 https://www.devze.com 2023-01-03 07:04 出处:网络
function send_mail( token, loader ) { $(\".send_mail\").bind( \"click\", function() { try{ var to = $(this).attr(\'ref\');
function send_mail( token, loader ) {
 $(".send_mail").bind( "click", function() {
  try{
   var to = $(this).attr('ref');
   var mail_form = $("#mail_form");
   mail_form.find("li:eq(0) input").val("sdsds");
   //mail_form.find("li:eq(0) input").attr("ref", "sdsds");
   //mail_form.find("li:eq(0) input").attr("value", "sdsds");
   $.fancybox(m开发者_StackOverflow社区ail_form.html(), { 'autoDimensions' : false, 'width' : 360, 'height' : 200, 'transitionIn' : 'none', 'transitionOut' : 'none', 'scrolling' : 'no', 'showCloseButton' : false });
   return false;
  }catch(err){alert(err);}
 });
}

My problem being that the above will not work yet if I use

//mail_form.find("li:eq(0) input").attr("ref", "sdsds");

it will change the ref and even

//mail_form.find("li:eq(0) input").attr("value", "sdsds");

will not work...

Any ideas whats happening here?


jQuery uses elem.value = ... and elem['value'] = ... when setting the value attribute using val() and attr, respectively. These methods don't change the actual HTML when it comes to the value attribute. Therefore, when you create the fancybox out of mail_form.html(), you're getting the original value attribute values, as they are still present in the HTML.

However, setAttribute() does change the underlying HTML value attribute. Try adding this after changing the values to sdsds and before calling $.fancybox(...):

mail_form.find("li:eq(0) input").each( function() {
    this.setAttribute("value",$(this).val());
});
0

精彩评论

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

关注公众号