【问题标题】:how to pass parametr to form urlAction?如何传递参数以形成 url 动作?
【发布时间】:2017-03-12 16:43:34
【问题描述】:

我得到了用于删除属性的 HttpPost:

        [HttpPost]
    public void RemoveProperty(int id)
    {
        propertyManager.Delete(id);
        Response.Redirect("EditProperties");
    }

在我的 cshtml 文件中,我得到了组合框,我需要获取选定的选项并将其作为 id 传递给 removeproperty,我的 cshtml 部分:

        <form action="@Url.Action("RemoveProperty", "Admin")" method="post" id="remove_property">
            <div class="form-group">
                <h3>Remove Property</h3>
                @*<input name="id" value="" id="removedid" />*@
                <select class="form-control" id="prop_id" name="id">

                    @foreach (var prop in Model)
                    {
                        <option value="Characteristic_Id">@prop.Id</option>

                    }
                </select>
            </div>
            <button onclick="" type="submit" class="btn btn-danger">Remove</button>
        </form>

我知道如何使用 jquery 从选定的组合框中获取文本:

$("#prop_id option:selected").text(); 但问题是如何将这个参数传递给表单??

【问题讨论】:

  • 同理,使用表单及其属性:$( "form" ).attr( "action", "new.url" );

标签: javascript html css url


【解决方案1】:
$("#prop_id")
    .change(function () {
        var str = "";
        $("#prop_id option:selected").each(function () {
            str += $(this).text() + " ";
        });
        $("#removeid").val(str);
    }).change();

解决方案!

【讨论】:

    【解决方案2】:

    您可以使用隐藏的输入字段。 还可以在选择框上使用 javascript 更改功能 更改选择框时,您可以将选定的文本放入隐藏字段。

    【讨论】:

    • 我做错了什么? ` $("#prop_id") .change(function () { var str = ""; $("#prop_id option:selected").each(function () { str += $(this).text() + " "; }); $("#removeid").text(str); }).change();`
    • 好吧,我只是忘了输入我需要设置 val 而不是文本
    • 替换这个 $("#removeid").text(str);
    • with $("#removeid").val(str);
    猜你喜欢
    • 2011-08-26
    • 1970-01-01
    • 2012-12-18
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多