【问题标题】:JQuery get / set value on hidden input elementJQuery获取/设置隐藏输入元素的值
【发布时间】:2016-08-18 09:29:04
【问题描述】:

我目前正在尝试使用 JQuery-ui、拖放 进行一些操作,直到我想根据拖入的内容更新隐藏字段值之前,一切似乎都正常工作可放置的目标。

简而言之,我正在尝试更新给定隐藏输入元素的值,例如:

<input type="hidden" id="Fixture1.HomeTeamId" name="Fixture1.HomeTeamId" value="">

并尝试在可拖动对象被放置到有效的可放置位置时执行此操作。 (请参阅下面的完整代码) 然而现在,我似乎根本无法从这些元素中获取任何价值。 当我将一个值添加到特定的隐藏输入元素中时。 例如:

<input type="hidden" id="Fixture1.HomeTeamId" name="Fixture1.HomeTeamId" value="20">

并在函数触发时尝试调用它 例如:

alert("Start Value: " + $("#Fixture1.HomeTeamId").attr("value"));
//OR    
alert("Start Value: " + $("#Fixture1.HomeTeamId").val());

它甚至返回“未定义”开始。

我想我要么忽略了某些东西,要么在左边或右边打了一个不太清楚的错字。但即使在 Stackoverflow 上浏览了一些类似的主题之后,我也没有在我的代码中发现问题......

希望有人能让我重回正轨!提前致谢。

作为一个 ASP.Net MVC 项目,在整个页面的剃须刀代码下方。

@model WebUI.Models.FixturesViewModel

<h2>Test</h2>

<div class="row">
    <div class="col-md-3">

        @foreach (var team in Model.Teams)
        {
            <div class="Draggable" name="Teams" data-TeamId="@team.TeamId" data-TeamName="@team.TeamName" data-CoachName="@team.CoachName">
                @team.TeamName
            </div>
        }

    </div>


    <div class="col-md-9">
        @using (Html.BeginForm())
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-horizontal">
                <h4>Fixtures</h4>
                <hr />

                <div>
                    <input type="hidden" id="Fixture1.HomeTeamId" name="Fixture1.HomeTeamId" value="20">
                    <input type="hidden" id="Fixture1.HomeTeamName" name="Fixture1.HomeTeamName" value="">
                    <input type="hidden" id="Fixture1.HomeCoachName" name="Fixture1.HomeCoachName" value="">
                    <input type="hidden" id="Fixture1.AwayTeamId" name="Fixture1.AwayTeamId" value="">
                    <input type="hidden" id="Fixture1.AwayTeamName" name="Fixture1.AwayTeamName" value="">
                    <input type="hidden" id="Fixture1.AwayCoachName" name="Fixture1.AwayCoachName" value="">

                    <div class="row">
                        <div class="col-md-5" data-position="Fixture1.Home">
                            <div class="Dropable">

                            </div>
                        </div>
                        <div class="col-md-2">
                            <p>Versus</p>
                        </div>
                        <div class="col-md-5" data-position="Fixture1.Away">
                            <div class="Dropable">

                            </div>
                        </div>
                    </div>

                </div>
                <hr/>
                <div class="row">
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Create" class="btn btn-default" />
                        </div>
                    </div>
                </div>
            </div>
        }
    </div>
</div>

@section scripts {
    <script>
        $(document).ready(function () {

            $(".Draggable").draggable({
                revert: "invalid",
                snap: ".Dropable",
                stack: ".Draggable",
                helper: "clone",
                cursor: "move"
            });


            $(".Dropable").droppable({
                accept: ".Draggable",
                drop: handleDropEvent
            });


            function handleDropEvent(event, ui) {
                //Set target variable to the droppable element
                var target = $(this);
                //Disable the droppable element (no longer a valid drop area)
                target.droppable("disable");
                //Add a button to remove
                target.parent().append("<div class='removeTeam btn btn-danger'><span class='glyphicon glyphicon-remove-circle'></span></div>");
                //Set dropped variable to the element dropped.
                var dropped = $(ui.draggable);
                //Take over the text value and add it to the container where dropped.
                target.text(dropped.text());
                //get the corresponding fixture
                var positionRef = target.parent().data("position");
                //set the hidden values where needed
                alert("Start Value: " + $("#Fixture1.HomeTeamId").val());
                $("#" + positionRef + "TeamId").val(dropped.data("teamid"));
                alert("#" + positionRef + "TeamId , should be set to " + dropped.data("teamid"));
                alert("End Value: " + $("#Fixture1.HomeTeamId").val());

                $("#" + positionRef + "TeamName").val(dropped.data("teamname"));
                $("#" + positionRef + "CoachName").val(dropped.data("coachname"));



            }
        });

        $("div").on("click", "div.removeTeam", function () {
            //Set the clicked variable to the element clicked on.
            var clicked = $(this);
            //Enable the 'container' above to allow elements to be dropped in again
            clicked.prev().droppable("enable");
            //Remove any text set
            clicked.prev().text("");
            //Remove the button
            clicked.remove();
            //get the corresponding fixture
            var positionRef = clicked.parent().data("position");
            //set the hidden values back to null
            $("#Fixture1.HomeTeamId").val("");
            $("#" + positionRef + "TeamName").val("");
            $("#" + positionRef + "CoachName").val("");
        });
    </script>
}

【问题讨论】:

    标签: jquery jquery-ui-draggable jquery-ui-droppable


    【解决方案1】:

    我认为您的问题来自您的选择器。您应该从选择器中转义点以便能够获取输入值。 (在 Jquery 中,点是用于选择类的保留字符)

    $(function() {
      var val = $("#Fixture1\\.HomeTeamId").val();
      alert("Start Value: " + val);
    });
    

    https://jsfiddle.net/dg81L29k/

    进一步阅读:

    https://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/

    【讨论】:

    • 确实完美!因为我也在使用“。”在元素的 id 中,是否建议不要这样做?随着代码的进一步下降,当我抓住现有的 Id(包含“。”)并向其添加后缀以查找与 Id 相关的内容时,它似乎也起作用了。
    • 如果您可以更改您的命名约定,您可以添加后缀而不是使用点。
    • 感谢@Quentin Roger 的快速见解以及进一步阅读文档。这有助于让我回到正轨!干杯!
    猜你喜欢
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2012-12-06
    • 1970-01-01
    相关资源
    最近更新 更多