簡體   English   中英

僅在選中復選框時顯示文本框,如果未選中,則不將字段保存在數據庫中

[英]Appear a textbox only when the checkbox is checked and not save the field in the database if is not checked

對不起,英語語法,不是我的母語。

我只想在選中復選框時顯示一個文本框。 所以,我有這段代碼:

HTML

<table>
    <tr>
        <td>
            <input type = "checkbox" id="NCFtxt" onclick="obtenerNCF()"> Marcar casilla para generar Comprobante Fiscal
         </td>
    </tr>
    <tr>
        <td>
            NCF: <input readonly="readonly" name= "txtNCF" type= "text" id= "txtNCF" value= "<?php echo $cobro->ncf; echo $ncf; ?>" />
        </td>
    </tr>
</table>  

JAVASCRIPT

<script language="javascript">
$("#txtNCF").hide();

function obtenerNCF() { 
  var chequear = document.getElementById("NCFtxt");    

  if (chequear.checked == true){
  $("#txtNCF").show();

  } else {
    $("#txtNCF").hide();
  }
}
</script>

因此,這是“有效的”,在選中復選框時隱藏了該字段,但仍將數據保存在數據庫中。 我該如何不僅隱藏字段,而且不保存在數據庫中? 我正在嘗試一些事情,但沒有任何效果,這似乎很簡單,但是卻使我感到困惑

如果您不想基於復選框保存某些內容...請查看$ _POST,如果復選框具有值(如果沒有,則未選中)。 然后保存或不保存多余的數據。

為復選框指定名稱和值:

<input type="checkbox" name="NCFtxt_checkbox" value="1" id="NCFtxt" onclick="obtenerNCF()">

並在PHP中進行查找:

if ( empty($_POST['NCFtxt_checkbox']) ) {
    // dont save extra data 
} else {
    // else do save the extra data
}

第二種解決方案是使用大量JavaScript,然后取消選中該復選框,然后在隱藏另一個文本框時清除它的內容。 這樣一來,它將被提交為“空”。

暫無
暫無

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

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