2016-06-23 9 views
-3

画像をアップロードするために使用しているtype="file"のhtml入力タグを無効にしようとしています。ラジオオプションを使用して「はい」または「いいえ」を示します。ユーザーが「いいえ」をクリックすると、入力テキストボックスが無効になります。これは私が使用したコードですが、私は何が間違っているのか分かりません。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

その型は、 '#のinputattrib'何ですか?' <入力のid = "画像" タイプ= "テキスト" ' –

+0

をファイルではありませんか – Rohit

+0

@モハマドあなたは完全に質問の男を破壊しました。 –

答えて

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を1として、作業コードは次のとおりです。

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

こんにちは良いもの:今は完全に今働いています。私の問題を解決するために大変感謝しています:あなたが送ったコードはここにあります:$( '[name = "pmethod"]')。change(function(){ else $( '#images')。attr( 'disabled'、 ')'; '無効'); }); – user3418506

+0

@ user3418506あなたがすでに使っているように、 'prop'を使う方が良いです。 –

+0

@ ZakariaAcharki OPが古いjQueryを使用していると思います。どう思いますか? –

0

Working fiddle

あなたのコードは、typeをスクリプトタグから削除するか、代わりにtype="text/javascript"に変更するだけです。

そして、あなたは名前で1時間にイベントを割り当て、代わりに2つのイベントが追加の条件として値を使用している場合、それは良くなる。このことができます

$('[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

JavaScriptが実行されなかった主な問題の1つであるtype属性を忘れてしまった。 –

関連する問題