简体   繁体   English

选择所选元素旁边的元素

[英]Select the element next the selected

I have next code: 我有下一个代码:

<tr>
  <td>
    <select class="select_big" name="additional_field" id="<?=$field['id'];?>" required>
      <option value="0">Выберите значение</option>
      ...
  </td>
</tr>
<tr>
  <td>
    <select class="select_big" name="additional_field" id="<?=$field['id'];?>" required>
      <option value="0">Выберите значение</option>
      ...
  </td>
</tr>
<tr>
  <td>
    <select class="select_big" name="additional_field" id="<?=$field['id'];?>" required>
      <option value="0">Выберите значение</option>
      ...
  </td>
</tr>
<tr>
  <td>
    <select class="select_big" name="additional_field" id="<?=$field['id'];?>" required>
      <option value="0">Выберите значение</option>
      ...
  </td>
</tr>

When I select the second "select" element, I need to disable all the next one. 当我选择第二个“选择”元素时,我需要禁用所有下一个元素。 If I select 1 need to disable 3 and 4, if I select 2 I need to disable only 4. Count of element could be different. 如果我选择1需要禁用3和4,如果我选择2我仅需要禁用4。元素数可能会不同。

How I can get all elements next the select? 我如何在选择之后获得所有元素?

I try next: 我接下来尝试:

<script type="text/javascript">
  $("[name=additional_field]").change(function(e) {
    field_data = '#' this.id.replace(/[* .]/g, "\\$&");
      $(field_data).parent().parent().nextAll().each(function(i) {
        console.log($(this)('[name=additional_field]'));
      });
  });
</script>

But I receive next error: Uncaught TypeError: object is not a function 但是我收到下一个错误:未捕获的TypeError:对象不是函数

Help me please. 请帮帮我。

I think you can do it simpler without confusing traversal of the parent nodes: 我认为您可以做到更简单,而又不会混淆父节点的遍历:

var $sel = $('.select_big').change(function() {
    $sel.filter(':gt(' + $sel.index(this) + ')').prop('disabled', +$(this).val());
});

Demo: http://jsfiddle.net/VFcF9/ 演示: http//jsfiddle.net/VFcF9/

It will also reenable selectboxes if you select default option back. 如果您选择返回默认选项,它还将重新启用选择框。

The error you were getting is because of this line: 您收到的错误是由于以下原因:

$(this)('[name=additional_field]')

The first part, $(this) , returns a jQuery object, and that object is not a function so you can't follow it with more parentheses. 第一部分$(this)返回一个jQuery对象,该对象不是函数,因此不能在其后加上更多的括号。

As for your requirement to disable all of the following select elements, perhaps: 至于您需要禁用以下所有选择元素的要求,也许是:

$("[name=additional_field]").change(function(e) {
  $(this).closest("tr").nextAll("tr").find("[name=additional_field]").prop("disabled", true);
});

This should do it 这应该做

$("[name=additional_field]").change(function(e) {

  var f = $(this).closest('tr')             //find the closest parent tr
                 .nextAll('tr')             //find all tr that follows
                 .find('select.select_big') //from them, get all select

  //f should be all the selects the follow what changed

});

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

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