在jquery

时间:2019-10-23 16:08:04

标签: jquery asp.net-mvc

我有一个下拉列表,当选中该列表时,需要更新URL,因此我可以通过jquery ajax发送此更新的URL,并获取更新的结果并将其发回。我现在遇到的问题是我无法通过我的jquery调用更改查询字符串参数值之一。我将如何去做。请参见下面。

<div class="col-sm-5">
                @{
                    var href = Url.Action<MassUploadIndexController>(c => c.LoadTemplateProperties(Model.ClientId, Model.UploadType, @property.Name, @property.SelectedValueId));
                }
                @if (!property.IsEnabled)
                {
                    @Html.UI().DropDownList($"Properties[{@property.Name}]", selectList: @property.Options.Select(o => new SelectListItem { Text = o.Text, Value = o.Value, Selected = o.Value == @property.SelectedValueId.ToString() }), htmlAttributes: new { @class = "propertySelector", @disabled = "disabled", @data_reloadUrl = href })
                }
                else
                {
                    @Html.UI().DropDownList($"Properties[{@property.Name}]", selectList: @property.Options.Select(o => new SelectListItem { Text = o.Text, Value = o.Value, Selected = o.Value == @property.SelectedValueId.ToString() }), htmlAttributes: new { @class = "propertySelector", @data_reloadUrl = href })
                }
            </div>

和脚本

$(document).on("change", ".propertySelector", function (e) {
            var link = $(this).attr('data-reloadUrl');
            link.val().replace("selectedValueId=00000000-0000-0000-0000-000000000000", $(this).val());
            alert(link);
            $.ajax({
                url: link,
                    success: function (data) {
                        var newHtml = $(data);
                        $("#selectpropertycontainer").html(newHtml);
                    }
            });
        });

我在控制台上看到的网址是 / site1 / client / 77089cca-c09d-419e-8978-72aef1133c8e / upload / loadproperties?uploadType = metadata&name = ProgramId&selectedValueId = 00000000-0000-0000-0000-000000000000

当下拉列表被选中时,我想要更改的值是selectedValueId。我想从下拉列表选择的值中将值更新为正确的GUID。

让我知道是否需要更多信息。 谢谢!

1 个答案:

答案 0 :(得分:0)

您的问题归结为以下几行:

var link = $(this).attr('data-reloadUrl');
link.val().replace("selectedValueId=00000000-0000-0000-0000-000000000000", $(this).val());
  • 当您使用.attr时,它将(在这种情况下)返回一个字符串,因此您不希望link.val().replace(
  • 当您使用.replace时,它不会就地替换,它会返回带有新值的新字符串,因此您需要在某个地方使用它
  • 如果将“ selectedValueId = [guid]”替换为“ [guid]”,则会丢失“ selectedValueId =“

给予:

var link = $(this).attr('data-reloadUrl')
                  .replace("selectedValueId=00000000-0000-0000-0000-000000000000",
                           "selectedValueId=" + $(this).val());