Im trying to set the values of my text boxes from a WebGrid im MVC 3. Im able to get the data using Jquery. the boxes populates for half a second the resets. Any ideas why?
$(".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 = Gr开发者_如何学JAVAoup.html();
document.getElementById("LevelDescription").value = Level.html();
document.getElementById("Date").value = effective.html();
document.getElementById("Amount").value = amount.html();
});
Prevent the default action.
$(".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();
});
If you don't have any particular reason to use document.getElementById(id)
use the jQuery equivelant $('#id')
it is much more powerfull and you're loading jQuery anyway.
Heres a bit of psuedo code that should give you an idea how to set the textbox value:
$('#textbox').val(value);
To get the current value you just us .val()
so without the argument. I hope this helps you.
Here is the documentation on the .val()
function
if whatever is a .select
is a button in a form, then its default action (submit) may be happening. Try a return false or preventDefault, like ...
$(".select").click(function(e) {
e.preventDefault();
...
$('#GroupDescription').attr('value', Group.html());
精彩评论