Reputation: 8944
In my ASP.net project, I am using a jquery script to update the id=person field in the URL bar:
$("form").append("<input type='hidden' name='hiddenEmployeeId' value='" + $('#EmployeeSelected').val() + "' />");
This is triggered when the value of the drop-down box changes. It works great, except for the fact that when you change it...it just appends the new id behind the first id, which is exactly what it's supposed to be doing.
However, I only want one id, obviously. What can I do here to accomplish this? Is there a way to CLEAR that first, and then append it?
Thanks!
Upvotes: 0
Views: 111
Reputation: 9031
Cant you just change the value on the input named 'hiddenEmployeeId' or does it not exists the first time?
$("input[name=hiddenEmployeeId]").val($('#EmployeeSelected').val());
you can do something like this:
$($('input[name=hiddenEmployeeId]')[0] || $('<input type="hidden" name="hiddenEmployeeId">').appendTo("form.selector")[0]).val($('#EmployeeSelected').val());
and change the "form.selector" to the form you are working with.
Upvotes: 1
Reputation: 82584
Check to see if it exists, if so, set the value. If it doesn't append it:
var input = $('input[name=hiddenEmployeeId]');
if(input [0]) {
input.val($('#EmployeeSelected').val());
} else {
$("form").append("<input type='hidden' name='hiddenEmployeeId' value='" + $('#EmployeeSelected').val() + "' />");
}
Upvotes: 3