簡體   English   中英

檢查文本輸入是否為空

[英]Check if text input is not empty

我確實有一個復選框和輸入字段,在選中復選框后啟用了該復選框。 我想要的是在用戶想要編輯記錄時使輸入字段已經啟用。 意味着在用戶選中復選框並填充輸入之后,以及在提交是否要修改它之后,應該已經啟用該字段。

我在javascript中擁有什么:

    $(document).ready(function () {
        $('.checkbox').change(function() {
          $(this).parent().next().find('.field').prop('disabled', !$(this).is(':checked'))
        });
    });

並展開復選框:

    <div class="row">
        <div class="small-8 large-8 columns">
        <label>Process: <small style="color:red;">*</small>         
            <div class="multiselect">               
                <div class="selectBox" onclick="showCheckboxes()">
                <select onclick="showCheckboxes()">
                    <option>-- Select an option --</option>
                </select>
                <div class="overSelect"></div>
                </div>
                <div class="scrollable" id="checkboxes">
                <?php 
                    while ($row = mysqli_fetch_array($result))
                    {
                        $row[0] = cleanOutputData($row[0]);

                        if(isset($process))
                        {
                            foreach($process as $code)
                            {
                                if($row[0] == $code)
                                {
                                    $match = 1;
                                    break;
                                }
                                else
                                    $match = 0;
                            }
                        }
                        if(isset($requiredNo) && isset($process))
                        {
                            foreach($process as $key => $code)
                            {
                                if($row[0] == $code && $requiredNo[$key] != 0)
                                {
                                    $match4 = 0;
                                    break;
                                }
                                else
                                    $match4 = 1;
                            }
                        }                           
                ?>  
                    <div class="row">
                        <div class="small-12 large-12 columns">
                        <label style="height: 37px; width:80%; float:left;" >
                        <input type='checkbox' class="checkbox" style="margin-left:5%; width:15%;" name='process[]' id=<?php echo $row[0] ?>  value="<?php echo $row[0] ?>" <?php if (isset($process) && $match==1) echo 'checked="checked"' ?>/><?php echo $row[0] ?>
                        </label>

                        <label style="width:40%; margin-left:60%;">
                        <input type="text" class="field" disabled style="width:40%;" name="numberpl" id="<?php echo $row[0] ?>" value= "<?php if (isset($requiredNo) && $match4==0) echo $requiredNo[$key] ?>" required/>
                        </label>
                        </div>
                    </div>

                <?php
                    }
                    mysqli_free_result($result);
                ?>  
                </div>
            </div>
        </label>
        </div>
    </div>

那么問題是,如果不為空,如何使該字段啟用? 謝謝

您可以嘗試在完成加載時觸發復選框:

$(document).ready(function () {
        $('.checkbox').change(function() {
          $(this).parent().next().find('.field').prop('disabled',!$(this).is(':checked'));
        });
$('.checkbox').trigger('change');
});

這可能對您有幫助,

If($('input.field').val().length > 0){
       $('input.field').prop('disabled', false);

}
if($('.field').val().length) {
    $('.field').prop('disabled', false);
}

您可以使用javascript中的value屬性來執行此操作:

if(document.getElementById('#yourinputid').value != ""){
  document.getElementById('#yourinputid').disabled = "false";
}

由於您正在循環播放,因此有很多輸入文本,因此請使用最接近的輸入文本

$(document).ready(function () {
    $('.checkbox').change(function() {
        if ($(this)[0].checked == true) {
            $(this).closest('.field').removeAttr('disabled');
        } else {
            $(this).closest('.field').attr('disabled', 'disabled');
        }
    });
});

用這個:

    $(document).ready(function () {

            if($('input[name="numberpl"]').val().length() != 0){
               $('input[name="numberpl"]').attr('disabled', false);
           }
            $('.checkbox').change(function() {
              $(this).parent().next().find('.field').prop('disabled', !$(this).is(':checked'))
            });
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM