简体   繁体   English

如何将HTML元素保存到jQuery对象?

[英]How do I save an HTML element(s) to a jQuery object?

I'm trying to add another one of these on an image click: 我正在尝试在图片点击上添加其中的另一个:

<div class="roleoption">
    <img src="@Url.Content("~/Public/images/delete.png")" alt="delete" />
    @Html.DropDownList("role", DSS.WebUI.Helpers.CustomHelpers.SelectListItemsForRole(Model.ExistingRoles, role))
</div>

And here I'm trying to create a new .roleoption whenever the add image is clicked: 在这里,只要单击添加图像,我就尝试创建一个新的.roleoption

<script language="javascript" type="text/javascript">
    $(document).ready(function () {

        //A roundabout way to construct the DIV I need:
        var $roleOption = '<div class="roleoption"><img src="@Url.Content("~/Public/images/delete.png")" alt="delete" /></div>';
        var $selectList = '@Html.DropDownList("role", DSS.WebUI.Helpers.CustomHelpers.SelectListItemsForRole(Model.ExistingRoles, 1))';
        $($roleOption).append($selectList);


        $('img.add-role').click(function() {
            $(this).parent().append($roleOption);
        });
    });
</script>

The MVC3 code you see up there, generates the following script on View render: 您在此处看到的MVC3代码在View渲染器上生成以下脚本:

<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        var $roleOption = '<div class="roleoption"><img src="/Public/images/delete.png" alt="delete" /></div>';
        var $selectList = '<select id="role" name="role"><option selected="selected" value="1">Anonimo</option>
<option value="2">Registrado</option>
<option value="3">Tecnico</option>
<option value="4">Empresario</option>
<option value="5">Editor</option>
<option value="6">Financias</option>
<option value="7">Administrador</option>
</select>';
        $($roleOption).append($selectList);
        $('img.add-role').click(function() {
            $(this).parent().append($roleOption);
        });
    });

</script>

I get the following error: 我收到以下错误:

Could not convert JavaScript argument arg 0 [nsIDOMDocumentFragment.appendChild] 无法转换JavaScript参数arg 0 [nsIDOMDocumentFragment.appendChild]

How can I make this work and how can I clean up this code? 我该如何进行这项工作以及如何清理此代码?

I think the issue lies in the fact that my HTML helper return the select element as multilines, how can I workaround this fact? 我认为问题出在我的HTML帮助器将select元素返回为多行这一事实,我该如何解决呢?

Edit: 编辑:

If I do: 如果我做:

var $selectList = $(' @Html.DropDownList("role", DSS.WebUI.Helpers.CustomHelpers.SelectListItemsForRole(Model.ExistingRoles, 1)) ');

I get: 我得到:

    $(document).ready(function () {
        var $roleOption = '<div class="roleoption"><img src="/Public/images/delete.png" alt="delete" /></div>';
        var $selectList = $(' <select id="role" name="role"><option selected="selected" value="1">Anonimo</option>
<option value="2">Registrado</option>
<option value="3">Tecnico</option>
<option value="4">Empresario</option>
<option value="5">Editor</option>
<option value="6">Financias</option>
<option value="7">Administrador</option>
</select> ');
        $($roleOption).append($selectList);

        $('img.add-role').click(function() {
            $(this).parent().append($roleOption);
        });
    });

I believe you are correct that the error is due to the line breaks returned by the server-side code because the resulting JavaScript is invalid since you can't have line breaks in a string literal like that. 我相信您是正确的,该错误是由于服务器端代码返回的换行符引起的,因为生成的JavaScript无效,因为您不能在这样的字符串文字中使用换行符。 You need to remove those line breaks on the server-side. 您需要在服务器端删除这些换行符。 I'm not sure of the exact syntax, but something like this: 我不确定确切的语法,但是像这样:

var $selectList = '@Html.DropDownList("role", DSS.WebUI.Helpers.CustomHelpers.SelectListItemsForRole(Model.ExistingRoles, 1)).Replace("\n","")';

You may need to chain an extra .Replace("\\r","") after the first replace. 第一次替换后.Replace("\\r","")您可能需要链接一个额外的.Replace("\\r","") And you may need a .ToString() before the first .Replace() . 并且第一个.Replace() 之前可能需要一个.ToString() .Replace()

Beyond that though I think you have another problem because I suspect the following code isn't doing quite what you think it is: 除此之外,尽管我认为您还有另一个问题,因为我怀疑下面的代码并没有完全按照您的想法做:

$($roleOption).append($selectList);
$('img.add-role').click(function() {
    $(this).parent().append($roleOption);
});

The $roleOption and $selectList variables are both strings. $roleOption$selectList变量都是字符串。 When you do this: 执行此操作时:

$($roleOption).append($selectList);

jQuery will parse $roleOption to create a new jQuery object and then append the $selectList to that object, but you don't actually save a reference to the resulting object in a variable at all. jQuery将解析$roleOption创建一个新的jQuery对象,然后将$selectList附加到该对象,但实际上您根本没有将对所得对象的引用保存在变量中。 So then within the .click handler when you append $roleOption it is just appending the original string. 所以那么内.click处理程序时,您将追加$roleOption它只是附加原始字符串。 Probably you meant to do this: 可能您打算这样做:

$roleOption = $($roleOption).append($selectList);

You should be able to wrap $() around the html and it will be parsed by jQuery. 您应该能够将$()环绕在html上,并且它将由jQuery解析。 I don't know exactly how MVC3 plays with this, but I would try: 我不知道MVC3怎么玩,但是我会尝试:

var $selectList = $(' @Html.DropDownList("role", DSS.WebUI.Helpers.CustomHelpers.SelectListItemsForRole(Model.ExistingRoles, 1)).Replace("\n", "") ');

Edit: Due to the fact that @Html.DropDownList() returns multiple lines, you would need to remove them by adding .Replace("\\n", "") . 编辑:由于@Html.DropDownList()返回多行,您需要通过添加.Replace("\\n", "")来删除它们。 I think. 我认为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM