获取文本框并在使用JQuery动态创建的表中下拉值

时间:2013-06-19 07:43:20

标签: jquery

请帮忙,我是jquery的新手。有3个表格将填充文本框并下拉“选择”动态

<table class="primaGridViewSmall" border="0" cellpadding="0" width="95%"
    id="shareAddUserMember" runat="server" clientidmode="Static" align="left">
</table>

<table class="primaGridViewSmall" border="0" cellpadding="0" width="95%"
    id="shareAddUserMemberA" runat="server" clientidmode="Static" align="left">
</table>

<table class="primaGridViewSmall" border="0" cellpadding="0" width="95%"
    id="shareAddUserMemberB" runat="server" clientidmode="Static" align="left">
</table>

该表可以有很多文本框和“选择”下拉,通过单击“添加更多”动态添加。 实际上我想从文本框中实现这样的东西并在该表中选择

var array = [];
var j = 0;
$('#shareAddUserMember tr').each(function () {
    var memberID = ?  // Dynamic_textbox_value;
    var privilage = ? // Dynamic_drop_down_selected value ie type <select>
    if (memberID != "") {
        array[j] = memberID + ":" + privilage;
        j++;
    }
});

我试过这个来获取textbox的值,我还需要选择

$('#shareAddUserMember tr').each(function () {
    $('input[type=text]').each(function (i, item) {
    var grade = $(item).val();
        alert(grade);
    });
});

但是上面的代码将获取页面中的所有输入类型。我有多个表。我需要在表shareAddUserMember中动态创建的文本框中获取值(id是动态生成的。)

我只需要在id =“shareAddUserMember”而不是shareAddUserMemberA或shareAddUserMemberB的表中使用textbox的值。

$('#shareAddUserMember tr').each(function () {
     // ---what to write here  to fetch the textbox and drop down selected values-----------
});

实际上我想从文本框中实现这样的东西,并在该表中输入选择类型

var array = [];
var j = 0;
$('#shareAddUserMember tr').each(function () {
    var memberID = ?  // Dynamic_textbox_value;
    var privilage = ? // Dynamic_drop_down_selected value ie type <select>
    if (memberID != "") {
        array[j] = memberID + ":" + privilage;
        j++;
    }
});

在tr中有下拉并选择样本DOM

<table class="primaGridViewSmall" border="0" cellpadding="0" width="95%"
    id="shareAddUserMember" runat="server" clientidmode="Static" align="left">
  <tr id="EmailIDUserRow1">
    <td style="text-align: center; width: 3%;">
      <input type="hidden" id="EmailUserMemberID1" name="EmailUserMemberID1" />
    </td>
    <td align="left">
      <select style="width: 150px" name="select1" id="select1">
        <option value="">Select</option>
        <option value="1">Full Access </option>
        <option value="0">View Only</option>
      </select>
    </td>
  </tr>
</table>

2 个答案:

答案 0 :(得分:0)

迭代#shareAddUserMember中的每个tr元素:

$("#shareAddUserMember tr").each(function() {
  // `this` refers to the current tr element

  // Get the input box and select that are inside the current tr element using
  // $(this).find()
  var memberID = $(this).find("input:text").val();
  var privilege = $(this).find("select").val();

  if (memberID !== "") {
    // ...
  }
});

希望有助于或至少指出正确的方向。

答案 1 :(得分:0)

试试这段代码:

$('#shareAddUserMember tr input[type=text]').each(function () {            
     // do some of your magic in here :)
});

既然你已经更新了你的答案,你可以尝试这样的事情(我没有测试这段代码)

$('#shareAddUserMember').each(function () {            
        var memberID = $('#shareAddUserMember tr input[type=text]').val();
        var privilage = $("#shareAddUserMember tr select option:selected").text();
        if (memberID != "") {
            array[j] = memberID + ":" + privilage;
            j++;
        }
    });