我遇到了一个关于replaceWith的问题,无法维持移动的单选按钮输入的状态。我准备了一个简单的示例来说明此问题。此功能适用于FF和Chrome,但不适用于IE。

有没有解决的办法?

谢谢!

jsbin:http://jsbin.com/unola4/2

码:

<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<title>IE replaceWith issue</title>
<script type='text/javascript'>
$(function(){
  $('a').click(function(e) {
    e.preventDefault();
    $('#temp').replaceWith($('#window').children());
  });
});
</script>
</head>
<body>
  <a href='#'>run replaceWith</a>
  <p>Select a radio button and then click "run replaceWith". The value persists in FF, but not IE.</p>
  <div id='window' style='background-color: #DDD; height: 100px;'>
    <input id="id_received_date-days_0" type="radio" name="received_date-days" value="30" />
    <input id="id_received_date-days_1" type="radio" name="received_date-days" value="50" />
    <input type='text' name='test-test' />
  </div>
  <br />
  <form id='foo' style='background-color: #EEE'>
    <div id='temp'></div>
  </form>
</body>
</html>

最佳答案

抓住被检查者的ID,然后在更换后再次检查它们:

$('a').click(function(e) {
    e.preventDefault();
    var checkedOnes = $("#window input:checkbox:checked").map(function() {
        return this.id;
    }).get();
    $('#temp').replaceWith($('#window').children());
    $.each(checkedOnes, function(index, value) {
        $("#" + value).attr("checked", "checked");
    });
});


同样,复制这些元素时,您将得到重复的ID。我假设您在替换后删除了旧的?重复的ID可能是问题的原因吗?

关于javascript - IE和replaceWith不保留单选按钮状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2767558/

10-16 13:40