通过Jquery根据下拉列表中的选择禁用数字字段


Disable Number field based on selection in dropdown via Jquery

我想做的是,如果用户选择"bycrypt",它应该显示一个字段来询问通过了多少次,如果他/她选择"sha512"来显示相同的字段,但被禁用。有人建议我使用Jquery执行此操作,但我的尝试不起作用。我的代码如下:

    <fieldset>
    <legend>Security</legend>
    <label>Hash Type</label>
    <select name="hash" id="myId">
            <option value="bcrypt"<?php if($hash == 'bcrypt') { echo ' selected="selected"'; } ?>>Bcrypt</option>
            <option value="sha512"<?php if($hash == 'sha512') { echo ' selected="selected"'; } ?>>SHA512</option>
        </select>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js" /></script>
    <label>Bcrypt rounds <i>(12 Rounds is recommended)</i></label>
    <script type="text/javascript>
    $("#myId").change(function() {
var tst = document.getElementById('myId').value;
if (tst ==1) {
    document.getElementById('#bcrypt_rounds').disabled=true;
} else {
    document.getElementById('#bcrypt_rounds').disabled=false;
}
});
    </script>
<input name="bcrypt_rounds" id="bcrypt_rounds" type="text" value="<?php echo $bcrypt_rounds; ?       >" />  

以上更新。现在剪切"bycrpt_rounds"标签之后的页面。无法访问"bycrypt_ounds"的字段。

类似这样的东西:

<script type="text/javascript">
$("#myId").change(function() {
    var tst = document.getElementById('myId').value;
    if(tst == "sha512") {
        document.getElementById('number_field_id').disabled=true;
    } else {
        document.getElementById('number_field_id').disabled=false;
    }
});
</script>

我认为这应该做

$("#myId").change(function() { 
    if($(this).val() == 'sha512'){
      $('#textBxId').attr({'disabled','disabled'});
    }else{
      $('#textBxId').removeAttr({'disabled'});
    }
});

找到错误,您只需在<script type="text/javascript"> 中忘记一个"

<fieldset>
    <legend>Security</legend>
    <label>Hash Type</label>
    <select name="hash" id="myId">
            <option value="bcrypt"<?php if($hash == 'bcrypt') { echo ' selected="selected"'; } ?>>Bcrypt</option>
            <option value="sha512"<?php if($hash == 'sha512') { echo ' selected="selected"'; } ?>>SHA512</option>
        </select>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js" /></script>
    <label>Bcrypt rounds <i>(12 Rounds is recommended)</i></label>
    <script type="text/javascript">
    $("#myId").change(function() {
var tst = $('myId').val();
if (tst ==1) {
    $('#bcrypt_rounds').attr('disabled','disabled');
} else {
    $('#bcrypt_rounds').removeAttr('disabled');
}
});
    </script>
<input name="bcrypt_rounds" id="bcrypt_rounds" type="text" value="<?php echo $bcrypt_rounds; ?       >" />