javascript - 选择 HTML 选择选项时启用按钮,反之亦然

标签 javascript jquery

我正在尝试做某事。我在 while 循环中有这个:

<tr>
<th>Order status</th>
<td>
<form class='update-status'>
<select name='status_id'class='status'>
<option selected='selected'>Order received</option>
<option value='2'>Processing</option>
</select>
<button type='submit' title='Click to update the status of this order' disabled>Update order status</button>
</form>
</td>
</tr>

我需要的是,只有选择了带有值2的选项,而反之亦然!我想我需要使用类似 $('select').on('change', function() 的东西,但我不知道如何获取选项的值。
我正在尝试:

$('.status').change(function()
{
    if ($('select option:selected').val() === 2)
    {
        $(this).closest('tr').find( 'button' ).prop("enabled", true);
    }
});

但这行不通。感谢您的帮助,谢谢!

最佳答案

尝试使用 .next()

Description: Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.

$(".status").change(function() {
  var button = $(this).next("button");
  if (this.value == "2") {
    button.prop("disabled", false);
  } else {
    button.prop("disabled", true)
  } 
});

$(".status").change(function(e) {
  e.preventDefault();
  e.stopPropagation();
  var button = $(this).next("button");
  if (this.value == "2") {
    button.prop("disabled", false);       
  } else {
    button.prop("disabled", true)
  };
  console.log(button.prop("disabled"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tbody>
    <tr>
      <th>Order status</th>
      <td>
        <form class='update-status'>
          <select name='status_id' class='status'>
            <option selected='selected'>Order received</option>
            <option value='2'>Processing</option>
          </select>
          <button title='Click to update the status of this order' disabled>Update order status</button>
        </form>
      </td>
    </tr>
  </tbody>
</table>

关于javascript - 选择 HTML 选择选项时启用按钮,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29835709/

相关文章:

javascript - 台式电脑中的 html5 geoLocation 错误

javascript - 使输入上的禁用属性与 Meteor 助手 react 的最佳方法是什么?

javascript - Flot - 如何使yaxis标签位置在水平条形图的中间?

javascript - 是否有一个预先写好的低影响时间下拉列表,允许选择一天中的 1440 分钟中的任何一个?

javascript - 从 jquery 代码中的数组中删除重复对象不起作用

javascript - 使用 raphael js 和 animate 一起创建一组路径

javascript - 正则表达式仅替换每个匹配项的第一次出现

javascript - leaflet Js 自定义控制按钮添加(文本、悬停)

javascript - jQuerys "hasClass"的反义词是什么?

javascript - Ajax json 响应未附加到页面