jQuery的克隆():这是一个错误与Firefox或我缺少的东西 [英] jQuery clone(): Is this a bug with firefox or I am missing something

查看:141
本文介绍了jQuery的克隆():这是一个错误与Firefox或我缺少的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $(document).ready(
function() {
alert($('#search')。val());
$ b var dummyInput = $('#search')。clone()
.attr(' (搜索查询)
$ b $('#search')。before(dummyInput);
}
) ;
$ b $(window).unload(function(){
$('#search_watermark')。remove();
});

和HTML:

 < form id ='test_form'action ='/ test.php'method ='post'> 
< label>建立新的团队< / label>< br />
< input type ='text'id ='search'/>
< / form>

问题是:

只将值赋给输入字段的克隆,而不是实际的输入字段。但是你会发现,在Firefox中,刷新页面时,实际输入字段的值已经改变。这种行为是意想不到的。



所有其他浏览器(FF除外)的行为与预期一致。我在XP上使用FF 3.5。

解决方案

我不得不围绕这个问题把我的大脑包裹起来,因为警报让我失望。给你的输入字段分配一些名字,当你做了这个克隆的时候,给它一个不同的名字,也就是说。+



$ p $ var code $ var dummyInput = $('#search')。clone()
.attr('id','search_watermark')
.val('Search query')。attr('name','foo');

Firefox会记住输入,与JS无关。如果您进行全面更新(+ Shift),则应该清除您发布的示例中的输入。


I have a problem with this jQuery code:

$(document).ready( 
  function(){  
    alert($('#search').val());  

    var dummyInput = $('#search').clone()
    .attr('id', 'search_watermark')
    .val('Search query')

    $('#search').before(dummyInput);
  }
);

$(window).unload(function(){
  $('#search_watermark').remove();
});

and the HTML:

<form id='test_form' action='/test.php' method='post'>
  <label>Create New Team</label><br/>
  <input type='text' id='search' />
</form>

The problem is:

We are assigning a value only to the clone of the input field, not the actual input field. But you will find that in Firefox, when you refresh the page, the value of the actual input field has changed. This behaviour is unexpected.

All other browsers (except FF) behaving as expected. I am using FF 3.5 on XP.

解决方案

I had to wrap my brain around the question because the alert kind of put me off. Assign some names to your input fields and when you do the clone, give it a different name, ie.+

var dummyInput = $('#search').clone()
.attr('id', 'search_watermark')
.val('Search query').attr( 'name', 'foo' );

Firefox will remember the input, it has nothing to do with JS. If you do a full refresh (+Shift) it should clear out the input from the example you posted as well.

这篇关于jQuery的克隆():这是一个错误与Firefox或我缺少的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆