简体   繁体   English

在文本之前在动态创建的选择选项元素中插入空格

[英]Insert space to dynamically created select option element before text

I need create kind of "treeview" look like select element by adding space to the option element text - <option name='City' /> . 我需要通过在选项元素文本- <option name='City' />添加空间来创建类似于select元素的“ treeview”。 My select creating dynamically this way: 我的选择以这种方式动态创建:

        $.ajax({
            url: "@Url.Content("~/Home/GetDestinationsTo")",
            data: { 'isRoundTrip': $("input[name='isrt']").is(":checked") ? 2 : 1 },
            type: "post",
            cache: false
        })
            .done(function (result) {
                if (this.Error == null) {
                    optionsto.append($("<option value=''>Куда...</option>"));
                    $.each(result, function () {
                        optionsto.append($("<option name='Country' class='bold info' />").val(this.CountryToId.CountryToId).text(this.CountryToName.CountryToName));
                        optionsto.append($("<option name='City' />").val(this.CityToId.CityToId).text(" " + this.CityToName.CityToName + " (" + this.CityToCode.CityToCode + ")"));
                    });
                } else {
                    $("#errormsg").text(result.Message);
                    $("#modalerror").modal();
                }
            })
            .fail(function (xhr, ajaxOptions, thrownError) {
                $("#errormsg").text(xhr.responseText);
                $("#modalerror").modal();
            });

Finally this must look like <option text=" SomeCityName"/> 最后,它必须类似于<option text=" SomeCityName"/>
I suppose only one way is to add HTML presentation of space - &nbsp; 我想只有一种方法是添加空格的HTML表示形式- &nbsp; because add just empty string not working - cut by browser. 因为添加仅空字符串不起作用-被浏览器剪切。 But it has been added like string, not like HTML and finally look like &nbsp;CityName 但是它像字符串一样添加,而不像HTML那样添加,最终看起来像&nbsp;CityName

If you only require one level of indent, you can try the <optgroup> tag like this: 如果只要求缩进一个级别,则可以尝试使用<optgroup>标记,如下所示:

<optgroup label="Group 1">
<option>option 1</option>
<option>option 1</option>
<option>option 1</option>
</optgroup>

<optgroup label="Group 2">
<option>option 1</option>
<option>option 1</option>
<option>option 1</option>
</optgroup>

...

Not the same thing exactly but it might work for you. 完全不一样,但可能对您有用。

Here is solution, if someone need this: 如果有人需要,这里是解决方案:

$('select#optionsto > option.city').each(function () {
     $(this).prepend("&nbsp;&nbsp;&nbsp;");
});

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

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