2016-10-18 59 views
0

我已經做了一個輸入框,我已經將此輸入框克隆到一個div,讓我們將它們命名爲ipbox1和ipbox2,ipbox2是ipbox1的副本現在我想要做的是,當我輸入/更改任何一個值其中,dom.value或$(「#id」)。val()應返回更新後的值。但現在它只能用ipbox2運行。 我該怎麼辦?如何影響原始輸入框中的更改?

fiddle

$("#ghj").click(function(){ 
 
\t $("#abc").val("Some text"); 
 
\t $("#abc").clone(true).appendTo("#pastehere"); 
 
}) 
 
$("#abc").on('change',function(){ 
 
\t alert(); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type = "text" id = "abc">This is the first box 
 
<div id = "pastehere"> 
 

 
</div>This is the cloned box 
 
<br><button type = "button" id = "ghj"> 
 
Clone 
 
</button>

+0

當您執行'$(#id).val()'時,您將獲得具有該ID的第一個元素的值。你能澄清我到底需要什麼嗎? – gschambial

+0

你可以在我的答案中查看你的問題更新的小提琴。 – gschambial

回答

0

如果你希望能夠獨立閱讀的其中一方的值,然後看看這個fiddle

如果沒有,那麼我一定是脫軌。請添加你需要實現:)

HTML

<input type = "text" class = "abc">This is the first box 
<div id = "pastehere"> 

</div>This is the cloned box 
<br><button type = "button" id = "ghj"> 
Clone 
</button> 

JS

var initialInput = $(".abc"); 
$("#ghj").click(function(){ 
    initialInput.val("Some text"); 
    initialInput.clone(true).appendTo("#pastehere"); 
}) 
$(".abc").on('change',function(ev){ 
    var targetInput = $(ev.target); 
    alert('myValueIs:' + targetInput.val()) 
}); 
1

我有更新,你有什麼更多的細節不甘示弱:https://jsfiddle.net/gschambial/s6td1bof/7/

var initVal = $('#abc').val(); 

$("#ghj").click(function(){ 
    $("#abc").val("Some text"); 
    $("#abc").clone(true).appendTo("#pastehere"); 
}) 
$("#abc").on('change',function(){ 
    alert(); 
    initVal = $(this).val(); 
}) 

$("#get").click(function(){ 
    alert(initVal); 
}) 
+0

與OP的代碼相同,無效的標記。 – Christophe

+0

@Christophe你檢查了小提琴嗎? – gschambial

+0

絕對,我確認該標記無效(請參閱我的答案)。 – Christophe

1

你不應該追加一個帶有id的克隆元素,因爲這會創建無效的標記(兩個具有相同id的html元素) 。

相關問題