开发者

MVC 2; How to copy a Html.EditorFor field in JQuery

I am trying to get used to the syntax in JQuery when using MVC 2.

I want to copy a phone number from 1 field to another. However the syntax of the line below is wrong; $("#contractAdministratorContact_Phone").val($("#contactClientContact_Phone").val());

The above fields are defined as

<%: Html.EditorFor(model => model.clientContact.Phone)%>
<%: Html.EditorFor(model => model.c开发者_开发问答ontractAdministratorContact.Phone)%>

When I look at the page source, the Ids are contractAdministratorContact_Phone and contactClientContact_Phone

My script I locate at the bottom of my page;

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $("#chkCopyContact").click(function () {
            if ($("#chkCopyContact").is(":checked")) {
                alert("in!");
                $("#contractAdministratorContact_Phone").val($("#contactClientContact_Phone").val());
            }
            alert("done!");
        })
    });
</script>

So what should my JQuery set statement be instead?


Try like this:

$("#contractAdministratorContact_Phone").val($("#clientContact_Phone").val());

Notice that the first textbox id should be clientContact_Phone and not contactClientContact_Phone as in your example (Assuming the Html helper code is correct). Also make sure that the :checked condition evaluates to true so that this code is executed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜