开发者

jquery append question

开发者 https://www.devze.com 2023-04-03 19:54 出处:网络
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=\'\" + $(\'#EmployeeSele

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!


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() + "' />");
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消