2011-05-11 62 views
1

我有一個div,我需要搶HTML的內容(所以自然我使用HTML())...使用HTML()獲取更改內容

但是,它們是文本字段。每當我抓住div(包含文本字段)的內容時,它抓住最初的HTML,而不是由最終用戶更改任何數據...

這裏是JS bin版本..更改輸入字段,運行該功能,你會看到,它只會採取該字段的初始值...

任何方法可以解決這個問題嗎?

http://jsbin.com/oleni3/edit

回答

0

http://jsbin.com/oleni3/5/edit

嘗試了這一點^

代碼:

$(document).ready(function() { 
    $('div#top input').change(function() { 
     $(this).attr('value', $(this).val()); 
    }); 
    $('#click').click(function() { 
    var _curHtml = $("div#top").html(); 
    $("div#bottom").html(_curHtml); 
    }); 
}); 
+0

哈哈山寨.... – 2011-05-11 14:52:17

0

您獲取div的HTML。你想獲得輸入框的值:

$(document).ready(function() { 
    $('#click').click(function() { 
     var _curHtml = $("#data").val(); 
     $("div#bottom").html(_curHtml); 
    }); 
}); 
+0

好點...基本上我的問題是我構建了一個內容切換器,它將HTML拖放到一個容器中進行查看,每個內容窗格都有很多輸入字段..所以我必須找出一些完整的東西不同... – stewart715 2011-05-11 15:00:02

0

沒辦法用html()來完成。您必須使用val()來獲取每個輸入的值。您可以使用

$("input").each(function() { 
    doSomething($(this).val()); 
} 

如果它使生活更輕鬆。

0
$(document).ready(function() { 
    $('#data').change(function() { 
    $(this).attr('value', $(this).val()); 
    }) 
    $('#click').click(function() { 
     var _curHtml = $("div#top").html(); 
     $("div#bottom").html(_curHtml); 
    }); 
}); 

這工作..這裏的鏈接http://jsbin.com/oleni3/2/edit

UPDATE:如果你有很多inputs在div裏面,你可以使用這個http://jsbin.com/oleni3/6/edit

0
$(document).ready(function() { 
    $('#click').click(function() { 
     var _curHtml = $("#data").val(); 
     $("div#bottom").html(_curHtml); 
    }); 
}); 
0

最簡單的黑客會是這樣的:

$("div#bottom input").val($("div#top input").val());

獲取更新輸入的值並將其設置爲克隆。

更新的鏈接:

http://jsbin.com/oleni3/3/edit