2016-10-21 7 views
1

私は、テキストボックスとチェックボックスのチェックを有効にし、チェックボックスをオフにしたときにテキストボックスとボタンの両方を無効にする必要があるという、私のUI上のシナリオを持っています。以下は、私が持っているもののスクリーンショットです:チェックボックスをオンにするとボタンを有効にしますか?

以下

enter image description here

私のJSPのコードです:

以下
<tr> 
    <td> 
    <stripes:checkbox name="locationselect" onclick="handleDisable(document.forms['dealerTransactionForm'].elements['locationselect'],document.forms['dealerTransactionForm'].elements['locationId'])"/> 
    <stripes:label for="locationId"/> 
    </td> 
    <td> 
    <stripes:text name="locationId" /> 
    <fmt:message var="tooltip" key="/DealerTransactionReport.action.lookUpLocation"/> 
    <stripes:submit name="lookUpLocation" class="button" title="${tooltip}" style="width: auto"/> 
    </td> 
</tr> 

私が書いたhandleDisable機能である:現在

function handleDisable(checkbox, item) { 
    if(checkbox.checked == true) 
     item.disabled = false; 
    else 
     item.disabled = true; 
} 

チェックボックスをオンにするとテキストボックスのみを有効にできます。テキストボックスとボタンの両方を有効にするにはどのような変更を行う必要がありますか。

+0

あなたはボタンの機能に三番目のパラメータに渡す必要があります。チェックボックスとテキストボックスを渡すだけです。しかしボタンはありません。 – user1289451

+3

[チェックボックスがオンの場合、チェックボックスを有効または無効にする方法](http://stackoverflow.com/questions/18110865/how-to-disable-enable-a-button-with-a-checkbox-if) -checked) –

+0

答えはありませんが、 'handleDisable'の最初のパラメータとして' this'を渡すことができます。また、私の意見では、議論のリストはあまりにも漠然としています。代わりに入力の名前を渡すのはどうでしょう。このように: 'handleDisable(this、 'locationId')'。フォームの名前をハードコードする必要があります。または、3番目のパラメータとして渡すこともできます。 'handleDisable( 'dealerTransactionForm'、this、 'locationId')' –

答えて

3

を削除しているので。チェックボックスとテキストボックスを渡すだけです。

HTML:

<stripes:checkbox name="locationselect" onclick="handleDisable(document.forms['dealerTransactionForm'].elements['locationselect'],document.forms['dealerTransactionForm'].elements['locationId'], document.forms['dealerTransactionForm'].elements['lookUpLocation'])"/> 

はJavaScript:

function handleDisable(checkbox, text, button) 
    { 
      if(checkbox.checked == true) { 
      text.disabled = false; 
      button.disabled = false; 
      } 
      else { 
      text.disabled = true; 
      button.disabled = true; 
      } 
    } 

しかし、それは私の意見では、HTMLは忙しすぎます。私はこのようなものを選ぶだろう。

HTML:

<stripes:checkbox name="locationselect" onclick="handleDisable(this)"/> 

はJavaScript:

function handleDisable(checkbox) 
    {    
      var text = document.getElementsByName("locationId")[0] 
      var button = document.getElementsByName("lookupLocation")[0] 

      if(checkbox.checked == true) { 
      text.disabled = false; 
      button.disabled = false; 
      } 
      else { 
      text.disabled = true; 
      button.disabled = true; 
      } 
    } 
+0

私はタイプミスがありました。やってみよう。 – user1289451

+0

'if..else..'ブロックを中括弧で囲む必要があります。 – Azim

+0

それを捕まえていない。 – user1289451

3

jQueryを使用すると、次のようにすることができます。

$('name="locationselect"').change(function() { 
    $('name="locationId"').prop('disabled', !this.checked); 
}); 

UPDATE: jQueryのタグがあなたのボタンの機能に三番目のパラメータを渡す必要があり

<script> 
 
    function handleDisable(elm) { 
 
     document.getElementsByName('locationId')[0].disabled = !elm.checked; 
 
     document.getElementsByName('lookUpLocation')[0].disabled = !elm.checked; 
 
    } 
 
</script> 
 

 
<input type="checkbox" name="locationselect" onclick="handleDisable(this)" /> 
 
<input type="text" name="locationId" disabled> 
 
<input type="submit" value="Submit" name="lookUpLocation" disabled>

+0

ありがとうAzim、これはうまくいった。 – Newbee

+0

「JS」がより簡単なものに変更されました。 @Newbee – Azim

関連する問題