2016-05-20 73 views
2

我有下面的代碼,其中,啓用和文本框的禁止是行不通的:文本框切換JQuery的

<!doctype html> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<title>TEST</title> 
<!-- JQuery 1.12.3 JS --> 
    <script src="../js/jquery-1.12.3.min.js"></script> 
</head> 
<body> 
    <table> 
     <tr> 
      <td><label class="control-label">Mode</label></td> 
      <td>&nbsp; 
       <label><input type="radio" name="payMode" value="cash" checked>Cash</label> 
       <label><input type="radio" name="payMode" value="cheque">Cheque</label> 
      </td> 
     </tr> 
     <tr> 
      <td><label>Cheque No</label></td> 
      <td><input type="number" name="chequeNo" pattern="^[0-9]" min="0" step="1" class="form-control" placeholder="Enter Cheque No" disabled></td> 
     </tr> 
     <tr> 
      <td><label>Cheque Date</label></td> 
      <td><input type="date" name="chequeDate" class="form-control" placeholder="Enter Cheque Date" disabled></td> 
     </tr> 
    </table> 
<script> 
<script> 
     $(':radio[name=payMode]').change(function() { 
    var prop = this.value == 'cash'; 
    $('input[name=chequeNo], input[name=chequeDate]').prop({ disabled: prop, required: !prop }); 
}); 
</script> 
</body> 
</html> 

基本上我試圖啓用基於無線點擊文本框2。如果用戶選擇支付模式作爲檢查,啓用檢查號和檢查日期文本框。如果用戶選擇現金,則禁用2個文本框。請指教

+2

[演示](https://jsfiddle.net/rpeyrs43/)工作正常。你得到什麼錯誤? – guradio

+0

@guradio你是對的,代碼工作正常。我有重複的腳本標籤,因此這個問題。 – Rajiv

回答

3

你有兩個起始腳本標記,內部腳本標記將導致一個javascript錯誤。

編輯

這裏是鏈接到的jsfiddle即證明了這一點,我只是刪除重複的標記。有時,簡單的答案是最好的:)下面

https://jsfiddle.net/0k98xnch/

<script> 
     $(':radio[name=payMode]').change(function() { 
    var prop = this.value == 'cash'; 
    $('input[name=chequeNo], input[name=chequeDate]').prop({ disabled: prop, required: !prop }); 
}); 
</script> 
+1

這絕不會回答這個問題。這應該是一個評論 –

+1

如果您不確定,請將其作爲評論張貼.. – Rayon

+0

那麼內部腳本標記將導致一個JavaScript錯誤,所以是的,它的確會回答這個問題。我怎麼能確定,我只知道這是一個問題,如果這不能解決它,他可以回覆,我會幫助更多 – Zach

2

示例顯示了當用戶改變「payMode」單選按鈕,啓用勾選否,檢查日期文本框,如果用戶選擇薪酬模式的檢查。如果用戶選擇現金,則禁用並清空2個文本框。

$("input[name=payMode]").on("change", function(){ 
 
    if($(this).val() == "cheque"){ 
 
    $("input[name=chequeNo], input[name=chequeDate]").attr("disabled", false); 
 
    }else{ 
 
    $("input[name=chequeNo], input[name=chequeDate]").val("").attr("disabled", true); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script> 
 
<!doctype html> 
 
<html lang="en"> 
 
<head> 
 
<meta charset="utf-8"> 
 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
 
<title>TEST</title> 
 
<!-- JQuery 1.12.3 JS --> 
 
    <script src="../js/jquery-1.12.3.min.js"></script> 
 
</head> 
 
<body> 
 
    <table> 
 
     <tr> 
 
      <td><label class="control-label">Mode</label></td> 
 
      <td>&nbsp; 
 
       <label><input type="radio" name="payMode" value="cash" checked>Cash</label> 
 
       <label><input type="radio" name="payMode" value="cheque">Cheque</label> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td><label>Cheque No</label></td> 
 
      <td><input type="number" name="chequeNo" pattern="^[0-9]" min="0" step="1" class="form-control" placeholder="Enter Cheque No" disabled></td> 
 
     </tr> 
 
     <tr> 
 
      <td><label>Cheque Date</label></td> 
 
      <td><input type="date" name="chequeDate" class="form-control" placeholder="Enter Cheque Date" disabled></td> 
 
     </tr> 
 
    </table> 
 
</body> 
 
</html>