[英]Using jQuery to disable input text box type=“file”
我正在尝试禁用用于上传图像的type="file"
的 HTML 输入标记。 我正在使用 Radio 选项来指示 Yes 或 No。如果用户单击 no - 它应该禁用输入文本框。 这是我使用的代码,但我不知道我做错了什么。
$('#inputattrib').change(function() { $('#images').prop('disabled', false); }); $('#inputattribf').change(function() { $('#images').prop('disabled', true); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="form-group"> <p> Indicate Payment Method:</p> <input id="inptrue" type="radio" name="pmethod" value="Yes">Yes <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
属性。 你有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');
});
您的代码是正确的,您只需从脚本标记中删除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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.