简体   繁体   English

单击按钮时如何获取选择文本,表格单元格值

[英]How to get select text, table cell value when a button is clicked

First off I'm new to html and javascript so please go easy :) 首先,我是html和javascript的新手,所以请放轻松:)

I have a table in html, in which each row has a dropdown (with the same options) and a button. 我在html中有一个表,其中每一行都有一个下拉列表(具有相同的选项)和一个按钮。 On clicking the button I would like to pass to a controller the text of the selected item in the dropdown/select along with another string. 单击按钮后,我想将下拉/选择中所选项目的文本以及另一个字符串传递给控制器​​。

So far I have 到目前为止,我有

        <table>
            <tr>
                <td>First name</td>
                <td>Surname</td>
            </tr>
            @foreach (var row in Model.Rows)
            {
                <tr>
                    <td>@row.FirstName</td>
                    <td>
                        <select id="surnames">
                            @foreach (var name in Model.Surnames)
                            {
                                <option value=@name.ToString()>@name.ToString()</option>
                            }
                        </select>
                    </td>
                    <td><input type="button" id="myButton" value="Go" onclick="location.href='@Url.Action("MyAction", "MyController", new { @firstName = row.FirstName, @newSurname = ??? })'" /></td>
                </tr>
            }
        </table>

        <script>
            function getSelectedVersion() {
                var version = document.getElementById("surnames").text;
            }
        </script>

Obviously, this is a trimmed down and modified version of my code. 显然,这是我代码的精简和修改版本。 In practise I have more properties in each Row in the Model. 实际上,我在模型的每个行中都有更多属性。

As I understand it I currently have an ID issue in that for all the rows I have the same id for the select. 据我了解,我目前存在一个ID问题,因为对于所有行,我的选择都具有相同的ID。 Short of adding a script for each row I can't think of a way round this, it would work (I think) but would be inelegant (a foreach in each row of the table, then one outside the table tags to allocate a script to each row's dropdown) 缺少为每行添加脚本的想法,我想不出什么办法,它可以工作(我认为),但效果不佳(在表的每一行中进行一次foreach,然后在表外部进行标记以分配脚本)到每一行的下拉菜单)

On top of that I'm missing the bit that calls my script to get the text and then pass it into the controller on the button click. 最重要的是,我缺少调用脚本以获取文本,然后单击按钮将其传递到控制器的位。

Ideally I would like to pass the entire row object to the controller, but I have a work around for that just now and just need the string firstName. 理想情况下,我想将整个行对象传递给控制器​​,但是现在我已经解决了这一问题,只需要字符串firstName即可。

There is only one Names collection in the model (as opposed to one in each Row in the model) that is used for each row in the table, because the options will always be the same for each table. 在表中,只有一个Names集合(而不是模型中的每个Rows)用于表中的每一行,因为每个表的选项始终相同。

Include jQuery at the top of your html code 在html代码顶部包含jQuery

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>

Then at the bottom of your code add this code: 然后在代码底部添加以下代码:

<script>
  $("input[type=button]").on("click", function(){ 
  var FIRSTNAME  = ($(this).parent().prev().prev()).html(); 
  //alert(FIRSTNAME);
  var   NEWSURNAME  = ($(this).parent().prev().children().attr("selected", "selected")).val(); 
  //alert(NEWSURNAME);
  $.post("/MyController/MyAction", { firstName: FIRSTNAME, newSurname: NEWSURNAME } );  
});
</script>

可能是这样吗?

<input type="button" onclick="location.href='@Url.Action("MyAction", "MyController", new {})' + '?' + 'newSurname='+getSelectedVersion()"/>

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

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