2017-08-28 28 views
0

我在我的應用程序中使用2 + 2單選按鈕。當我點擊任何單選按鈕時,它會影響一些輸入字段。但我很困惑,使用jquery.i已經搜索堆棧溢出使用click事件。有太多的建議,我看到這麼多的建議後感到困惑。我嘗試了一些,但沒有奏效。 這是我的HTML代碼找不到在jquery中的單選按鈕事件

<input type="radio" id="voucher" name="voucher" value="1" 
     checked="checked">Payment Voucher <input type="radio" 
     name="voucher" id="voucher" value="2">Receive Voucher 

    <input type="radio" name="transaction" id="transaction" 
             value="1" checked="checked">Cash <input type="radio" name="transaction" id="transaction" value="2">Bank 

而且我想這jQuery代碼:

$("input").on("click", function() { 
    if ($("input:radio[name=voucher]").is(":checked")) { 
     check = $('input:radio[name=voucher]:checked').val(); 
     alert(check); 
     if (check == 1) { 

      $("#credit").prop("disabled", true); 
      $("#debit").prop("disabled", false); 
     } else if (check == 2) { 

      $("#debit").prop("disabled", true); 
      $("#credit").prop("disabled", false); 
     } 
    } 
}); 

$("#transaction").click(function(event) { 
    if ($("input:radio[name=transaction]").is(":checked")) { 
     checktransaction = $('input[name=transaction]:checked').val(); 

     if (checktransaction == 1) { 

      $("#bankacc").prop("disabled", true); 
      $("#chequeno").prop("disabled", true); 
      $("#cashinhand").prop("disabled", false); 
     } 

     if (checktransaction == 2) { 

      $("#cashinhand").prop("disabled", true); 
      $("#bankacc").prop("disabled", false); 
      $("#chequeno").prop("disabled", false); 
     } 
    } 
}); 

這是片段:

$("input").on("click", function() { 
 
     if ($("input:radio[name=voucher]").is(":checked")) { 
 
      check = $('input:radio[name=voucher]:checked').val(); 
 
      alert(check); 
 
      if (check == 1) { 
 

 
       $("#credit").prop("disabled", true); 
 
       $("#debit").prop("disabled", false); 
 
      } else if (check == 2) { 
 

 
       $("#debit").prop("disabled", true); 
 
       $("#credit").prop("disabled", false); 
 
      } 
 
     } 
 
    }); 
 

 
    $("#transaction").click(function(event) { 
 
     if ($("input:radio[name=transaction]").is(":checked")) { 
 
      checktransaction = $('input[name=transaction]:checked').val(); 
 

 
      if (checktransaction == 1) { 
 

 
       $("#bankacc").prop("disabled", true); 
 
       $("#chequeno").prop("disabled", true); 
 
       $("#cashinhand").prop("disabled", false); 
 
      } 
 

 
      if (checktransaction == 2) { 
 

 
       $("#cashinhand").prop("disabled", true); 
 
       $("#bankacc").prop("disabled", false); 
 
       $("#chequeno").prop("disabled", false); 
 
      } 
 
     } 
 
    });
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
 
<input type="radio" id="voucher" name="voucher" value="1" 
 
    \t checked="checked">Payment Voucher <input type="radio" 
 
    \t name="voucher" id="voucher" value="2">Receive Voucher 
 
         
 
    <input type="radio" name="transaction" id="transaction" 
 
    \t \t \t \t \t \t \t \t \t value="1" checked="checked">Cash <input type="radio" name="transaction" id="transaction" value="2">Bank

+0

您的代碼出現錯誤。我真的不明白你想要做什麼? – Steven

+0

什麼是您切換禁用屬性的所有元素?這與兩對收音機有什麼關係?而且你多次使用ID。這在任何方面都是不正確的。 – hallleron

+0

我其實只是想知道我必須使用哪個單選按鈕單擊事件。抱歉錯誤的代碼。我只是保持我的代碼的主要部分這就是爲什麼有一些錯誤。 –

回答

0

你應該改變id來類(id是唯一的)並捕獲偶數t來獲得目標。看到片段: 希望它有幫助。 問候

$(".voucher").on("click", function(event) { 
 
      var target = $(event.target); 
 
     if (target.is(":checked")) { 
 
      check = target.val(); 
 
      alert(check); 
 
      if (check == 1) { 
 

 
       $("#credit").prop("disabled", true); 
 
       $("#debit").prop("disabled", false); 
 
      } else if (check == 2) { 
 

 
       $("#debit").prop("disabled", true); 
 
       $("#credit").prop("disabled", false); 
 
      } 
 
     } 
 
    }); 
 

 
    $(".transaction").click(function(event) { 
 
    var target = $(event.target); 
 
     if (target.is(":checked")) { 
 
      checktransaction = target.val(); 
 
      alert(checktransaction); 
 
      if (checktransaction == 1) { 
 

 
       $("#bankacc").prop("disabled", true); 
 
       $("#chequeno").prop("disabled", true); 
 
       $("#cashinhand").prop("disabled", false); 
 
      } 
 

 
      if (checktransaction == 2) { 
 

 
       $("#cashinhand").prop("disabled", true); 
 
       $("#bankacc").prop("disabled", false); 
 
       $("#chequeno").prop("disabled", false); 
 
      } 
 
     } 
 
    });
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 
 
<input type="radio" class="voucher" name="voucher" value="1" 
 
    \t checked="checked">Payment Voucher <input type="radio" 
 
    \t name="voucher" class="voucher" value="2">Receive Voucher 
 
         
 
    <input type="radio" name="transaction" class="transaction" 
 
    \t \t \t \t \t \t \t \t \t value="1" checked="checked">Cash <input type="radio" name="transaction" class="transaction" value="2">Bank

+0

謝謝。有用。我檢查了一個單選按鈕。當我重新加載它沒有顯示1被檢查。如何解決這個問題呢 ? –

+0

你可以使用一個唯一的ID來獲得初始值..區域 – Albeis

+0

請你能更詳細地解釋它嗎? –

0

您可以使用切換功能,以獲得檢查單選按鈕的值,如下面的代碼

$('input[name="voucher"]') 
.change(function(){ 
    if($(this).is(":checked")){ 
     var val = $(this).val(); 
     //alert(val) 

     if (val == 1) { 

     $("#credit").prop("disabled", true); 
     $("#debit").prop("disabled", false); 
    } else if (val == 2) { 

     $("#debit").prop("disabled", true); 
     $("#credit").prop("disabled", false); 
    } 

    } 
}); 


$('input[name="transaction"]') 
.change(function(){ 
    if($(this).is(":checked")){ 
     var val = $(this).val(); 
     if (val == 1) { 

      $("#bankacc").prop("disabled", true); 
      $("#chequeno").prop("disabled", true); 
      $("#cashinhand").prop("disabled", false); 
     } 

     if (val == 2) { 

      $("#cashinhand").prop("disabled", true); 
      $("#bankacc").prop("disabled", false); 
      $("#chequeno").prop("disabled", false); 
     } 
    } 
}); 

我還添加了一個工作小提琴也被加入您的幫助看下面的小提琴

WORKING FIDDLE

+0

謝謝。有用。我檢查了一個單選按鈕。當我重新加載這個頁面時,它沒有先顯示我爲選中的單選按鈕做了什麼,之後再次檢查它的工作。如何解決這個問題呢 ? –