2011-09-20 73 views
0

我试图设置我的文本框的值从WebGrid im MVC 3.我能够使用Jquery获取数据。这些盒子会重置半秒。任何想法为什么?设置来自jquery的文本框值

$(".select").click(function() { 
    var contribution = $(".select"); 
    var parent = contribution.parent(); 
    var children = parent.children(); 
    var Group = children.filter(':nth-child(2)'); 
    var Level = children.filter(':nth-child(3)'); 
    var effective = children.filter(':nth-child(6)'); 
    var amount = children.filter(':nth-child(7)'); 
    //alert(amount.html()); 
    document.getElementById("GroupDescription").value = Group.html(); 
    document.getElementById("LevelDescription").value = Level.html(); 
    document.getElementById("Date").value = effective.html(); 
    document.getElementById("Amount").value = amount.html(); 
}); 

回答

4

阻止默认动作。

$(".select").click(function (e) { 
    var contribution = $(".select"); 
    var parent = contribution.parent(); 
    var children = parent.children(); 
    var Group = children.filter(':nth-child(2)'); 
    var Level = children.filter(':nth-child(3)'); 
    var effective = children.filter(':nth-child(6)'); 
    var amount = children.filter(':nth-child(7)'); 
    //alert(amount.html()); 
    document.getElementById("GroupDescription").value = Group.html(); 
    document.getElementById("LevelDescription").value = Level.html(); 
    document.getElementById("Date").value = effective.html(); 
    document.getElementById("Amount").value = amount.html(); 
    e.preventDefault(); 
}); 
+0

你可能已经用'e.preventDefault();'很好的捕获它。 Upvoted;) – sg3s

1

如果你没有任何特别的理由使用document.getElementById(id)使用jQuery的equivelant $('#id')它是更强大的和你反正加载jQuery的。

继承人位的伪代码,应该给你一个想法如何设置文本框的值:

$('#textbox').val(value); 

为了让你的当前值只有我们.val()没有这样的说法。我希望这可以帮助你。

Here is the documentation on the .val() function

0
$('#GroupDescription').attr('value', Group.html()); 
1

如果不管是.select是一个形式的按钮,那么它的默认操作(提交)可能会发生。尝试一回虚假或了preventDefault,就像...

$(".select").click(function(e) { 
    e.preventDefault(); 

...