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

时间:2012-09-07 12:58:10

标签: php javascript 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_rounds”的字段。

3 个答案:

答案 0 :(得分:0)

这样的事情:

<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>

答案 1 :(得分:0)

我认为应该这样做

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

答案 2 :(得分:0)

发现错误,您忘记了"

中的<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; ?       >" />  
相关问题