简体   繁体   English

javascript下拉列表和功能

[英]javascript dropdowns and functions

I'm a beginner in web delpmnt. 我是网络delpmnt的初学者。 I need to pass id of a dropdown(html.dropdown) for a javascript function(OpenPopupForSelectedFilters), but what I tried is not working.Funtion is not receiving correct id. 我需要为JavaScript函数(OpenPopupForSelectedFilters)传递一个dropdown(html.dropdown)的ID,但是我尝试的方法不起作用。功能未收到正确的ID。 Let me know the correct way 让我知道正确的方法

My code: 我的代码:

<tr>
    @for (var i = 0; i < (@ViewData["AdditionalMaster"] as List<SelectListItem>).Count; i++)
    {
        var item = (@ViewData["AdditionalMaster"] as List<SelectListItem>)[i];
        <tr>   
            <td class="formLabel" style="text-align: right; padding-right: 50px">
                <label>@item.Text</label>
            </td>
            <td class="appliedFilter formField" style="text-align: left; width: 300px; padding-right: 50px">
                @Html.DropDownList("AdditionalMaster" + @item.Value, null, null, new { @Id ="Role"+(@item.Text).Replace(" ",""), @class = "dropdownStyle multiselect ", @multiple = "multiple", @Style = " width:275px;display:none" })
                <a href="javascript:OnClick=OpenPopupForSelectedFilters('Role'+@item.Text.ToString());"style="width: 20px; height: 13px" title="Show Selected Role">

JavaScript function: JavaScript函数:

OpenPopupForSelectedFilters = function (val) { debugger;
    var id = '#' + val;
    var ddlText = $(id).multiselect("getChecked").map(function () {
        return this.value;
    }).get();

    if (ddlText.length == 0)
        document.getElementById('seldFitlers').innerText = "No record(s)";
    else
        document.getElementById('seldFitlers').innerText = $.map($(id + ' :selected'), function (e) { return $(e).text(); }).join('\n');

    $("#dv_forSeldFilters").dialog({ title: 'Selected record(s)' });
    $('#dv_forSeldFilters').dialog('open');
    $('#dv_forSeldFilters').scrollTop(0);
}

In the following line: 在以下行中:

<a href="javascript:OnClick=OpenPopupForSelectedFilters('Role'+@item.Text.ToString());"

You need to encase the @item.Text.ToString() in quotes and remove the "OnClick=" like so: 您需要将@ item.Text.ToString()括在引号中,并删除“ OnClick =“,如下所示:

<a href="javascript:OpenPopupForSelectedFilters('Role'+'@item.Text.ToString()');"

The reason you need to do this is that the ASP.NET MVC rendering engine will evaluate @item.Text.ToString() to a text literal, not a string. 您需要执行此操作的原因是ASP.NET MVC呈现引擎会将@ item.Text.ToString()评估为文本文字,而不是字符串。 This means in your version it will end up looking like this: 这意味着在您的版本中它将最终看起来像这样:

<a href="javascript:OpenPopupForSelectedFilters('Role'+MyRole);"

Rather than what you're wanting which is this: 而不是您想要的是什么:

<a href="javascript:OpenPopupForSelectedFilters('Role'+'MyRole');"

Removing the "OnClick=" is more just because it's not necessary. 删除“ OnClick =“只是因为没有必要。

if the dropdown id is "Role"+(@item.Text).Replace(" ","") 如果下拉id"Role"+(@item.Text).Replace(" ","")

then you can render the anchor as 然后您可以将锚呈现为

<a href="javascript:OpenPopupForSelectedFilters('Role@(item.Text.Replace(" ",""))')"></a>

there is no need for the + and you are removing spaces in the dropdown id above. 不需要+并且您要删除上面的下拉id空格。

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

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