【问题标题】:Update ajax url parameter value in jquery更新jquery中的ajax url参数值
【发布时间】:2019-10-23 16:08:04
【问题描述】:

I have a dropdownlist which when selected, needs to update the url, so I could send this updated url via jquery ajax and get the updated result and post it back.我现在遇到的问题是我无法通过我的 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。

如果您需要更多信息,请告诉我。 谢谢!

【问题讨论】:

标签: jquery asp.net-mvc


【解决方案1】:

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

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());

【讨论】:

  • 非常感谢。那行得通!也为解释 +1。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 1970-01-01
相关资源
最近更新 更多