1

ラジオボタンが選択されたときに選択ドロップダウンのセットをアクティブにし、別のボタンが選択されたときに非アクティブにしようとしています。

jQuery:

$(':radio[name!="hours"]').change(function() {
  if ($(this).filter(':checked').val() == "open") {
    $(this).nextUntil("input","select").attr("disabled", false);
  } else {
    $(this).nextUntil("input","select").attr("disabled", true);
  }
});

HTML:

<form id="providerForm">
  <p><input type="radio" name="hours" value="no" checked="true"> There are no hours at this location</p>
  <p><input type="radio" name="hours" value="yes"> Enter the hours below</p>
  <span id="hoursList">
    <p><label for="monday">Monday: </label><span class="radios"><input type="radio" name="monday" value="closed"/> Closed</span>
    <span class="radios"><input type="radio" name="monday" value="open"/> Open <select name="monStart" id="monStart" disabled></select> to <select name="monEnd" id="monEnd" disabled></select></span></p>
    <p><label for="tuesday">Tuesday: </label><span class="radios"><input type="radio" name="tuesday" id="tueClosed" value="closed"/> Closed</span>
    <span class="radios"><input type="radio" name="tuesday" value="open"/> Open <select name="tueStart" id="tueStart" disabled></select> to <select name="tueEnd" id="tueEnd" disabled></select></span></p>
  </span>
  <input type="submit" id="loginButton" name="submit" value="Add Hours" />
</form>

フィドルはこちら: http://jsfiddle.net/BN6JD/

「開く」をクリックすると、選択ボックスが有効になります。これは素晴らしいことです。ただし、「閉じる」をクリックしても再び無効になることはありません。どこが間違っていますか?

4

1 に答える 1

1

checkedラジオ グループではなく、単一のラジオ入力 (である) をフィルタリングしてnextUntillおり、現在のマークアップでは機能しない、選択した要素の次の兄弟のみを選択します。また、プロパティを変更するpropには、 の代わりにメソッドを使用する必要がありattrます。これを試して:

$('input[type=radio][name!="hours"]').change(function() {
    $(this).closest('p') // select the closest parent `p` element
           .find('select') // find `select` elements
           .prop("disabled", this.value === 'closed'); // disable/enable the selects  
});

http://jsfiddle.net/6ELcA/

于 2013-07-11T22:11:03.093 に答える