2016-06-23 50 views
-3

我試圖禁用一個html輸入標籤type="file",我正在使用上傳圖像。我使用無線電選項來指示是或否。如果用戶單擊否,它應該禁用輸入文本框。這是我使用的代碼,但我不知道我做錯了什麼。使用jquery禁用輸入文本框類型=文件

<html> 
<head> 
<title>test</title> 
<script src="assets/js/jquery-1.11.1.min.js"></script> 

</head> 
<body> 
    <div class="form-group" style=""> 
    <p> Indicate Payment Method:</p> 
    <input id ="inptrue" type="radio" name="pmethod" value="Yes">Yes<b> 
    <input id="inpfalse" type="radio" name="pmethod" value="No">No; 
    </div> 

    <div class="form-group"> 
    <label for="file">Upload your Payment Evidence: <i>for Bank Deposit only</i></label> 
    <input id="images" type="text" name="file" class="form-first-name form-control"> 
    </div> 

    <script type="javascript/text"> 

    $('#inputattrib').change(function() { 
     $('#images').prop('disabled', false); 
    }); 
    $('#inputattribf').change(function() { 
     $('#images').prop('disabled', true); 
    }); 
    </script> 
</body> 
</html> 
+0

它的類型不是文件'<輸入ID = 「圖像」 類型= 「文本」' –

+0

是什麼'#inputattrib'? – Rohit

+0

@Mhamhammad你已經完全破壞了問題的傢伙。 –

回答

0

主要問題是具有<script>type屬性。你有type="javascript/text"這是不允許它的工作。

在更輕的筆記上,您已使用type="text"。你的意思是,type="file"

確保您有assets/js/jquery-1.11.1.min.js在正確的位置。

另外,沒有與選擇器#inputattrib匹配的元素。這是它不起作用的原因。您需要使用以下方法:

<html> 
    <head> 
    <script src="https://code.jquery.com/jquery-2.2.4.js"></script> 
    <title>test</title> 
    </head> 
    <body> 
    <div class="form-group" style=""> 
     <p> Indicate Payment Method:</p> 
     <input id ="inptrue" type="radio" name="pmethod" value="Yes">Yes<br /> 
     <input id="inpfalse" type="radio" name="pmethod" value="No">No; 
    </div> 

    <div class="form-group"> 
     <label for="file">Upload your Payment Evidence: <i>for Bank Deposit only</i></label> 
     <input id="images" type="text" name="file" class="form-first-name form-control" /> 
    </div> 
    <script> 
    $('#inptrue').click(function() { 
     $('#images').prop('disabled', false); 
    }); 
    $('#inpfalse').click(function() { 
     $('#images').prop('disabled', true); 
    }); 
    </script> 
    </body> 
</html> 

作業輸出:http://output.jsbin.com/vijinukape

按了OP,工作代碼爲:

$('[name="pmethod"]').change(function() { 
    if ($(this).val() === "Yes") 
    $('#images').removeAttr('disabled'); 
    else 
    $('#images').attr('disabled', 'disabled'); 
}); 
+0

嘿好的一個:現在它的工作現在完美了。這裏是幫助我在這裏的人回答的代碼:感謝您解決我的問題很多:這裏是你發現的代碼:$('[name =「pmethod」]')。change(function(){ ('(images)')。($(this) 'disabled'); }); – user3418506

+0

@ user3418506使用'prop'更好,因爲您已經使用過了。 –

+0

@ ZakariaAcharki我想這是OP使用舊的jQuery。你怎麼看? –

0

Working fiddle

您的代碼是正確的,您只需從腳本標記中刪除type,或將其更改爲type="text/javascript"

而且如果按名稱指定事件一次,使用值作爲條件,而不是添加兩個事件會更好:

$('[name="pmethod"]').change(function() { 
    if($(this).val()=="Yes") 
     $('#images').removeAttr('disabled'); 
    else 
     $('#images').attr('disabled','disabled'); 
}); 

希望這有助於。


$('[name="pmethod"]').change(function() { 
 
    if($(this).val()==="Yes") 
 
    $('#images').removeAttr('disabled'); 
 
    else 
 
    $('#images').attr('disabled','disabled'); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script> 
 

 
<div class="form-group" style=""> 
 
    <p> Indicate Payment Method:</p> 
 
    <input id ="inptrue" type="radio" name="pmethod" value="Yes">Yes<b> 
 
    <input id="inpfalse" type="radio" name="pmethod" value="No">No;      
 
    </div> 
 
    <div class="form-group"> 
 
    <label for="file">Upload your Payment Evidence: <i>for Bank Deposit only</i></label> 
 
    <input id="images" type="text" name="file" class="form-first-name form-control"> 
 
    </div>

+0

您忘了告訴type屬性,這是JavaScript的主要問題之一,JavaScript沒有執行。 –

相關問題