简体   繁体   English

当用户选择其他选项时,添加另一个文本框

[英]Add another text box when a user select other option

My form is looking like this. 我的表格看起来像这样。

<form action="add_customers.php" method="post"     onsubmit="MM_validateForm('name','','R','passport_no','','R','remarks','','R');return     document.MM_returnValue">
  <tr>
    <td>Name of the Applicant:</td>
    <td><label for="name"></label>
    <input type="text" name="name" id="name" /></td>
  </tr>
  <tr>
     <td>Country applying for :</td>
    <td><label for="country"></label>
      <select name="country" id="country">
        <option>Singapore</option>
        <option>Malaysia</option>
        <option>Istanbul</option>
         <option>Thailand</option>
        <option>China</option>
        <option>Other</option>
    </select></td>
  </tr>
  <tr>
    <td>Visa Categeory :</td>
    <td><label for="visa_categeory"></label>
      <select name="visa_categeory" id="visa_categeory">
        <option>Visit</option>
        <option>Business</option>
        <option>Other</option>
    </select></td>
  </tr>
  <tr>
    <td>Passport No:</td>
    <td><input type="text" name="passport_no" id="passport_no" /></td>
  </tr>
    <tr>
    <td>Remarks:</td>
    <td><label for="remarks"></label>
    <textarea name="remarks" id="remarks" cols="45" rows="5"></textarea></td>
  </tr>
  <tr>
     <td>&nbsp;</td>
    <td><input type="submit" name="submit" id="submit" value="Submit" /></td>
  </tr></form>

Now My problem is 1) when a user select other option from Visa Category then automatically display a text box for user and capture that what he enter. 现在我的问题是1)当用户从“签证类别”中选择其他选项然后自动为用户显示一个文本框并捕获他输入的内容时。 2) save that details into mysql database 2)将详细信息保存到mysql数据库中

Used languages are PHP,MYSQL 使用的语言是PHP,MYSQL

Thanks. 谢谢。

You can add a text box in your form and make it hidden first time. 您可以在表单中添加一个文本框,并使其首次隐藏。
Get the textbox visible on change of the dropdown you have mentioned using javascript. 在使用javascript更改下拉菜单时,使文本框可见。
You can get the value of the textbox in the page where you accepts the data and insert into data database in add_customers.php along with other variables. 您可以在接受数据的页面中获取文本框的值,并将其与其他变量一起插入add_customers.php中的数据数据库。

<script>
function visacatOnchange(){
    var visa_cat = document.getElementById('visa_categeory').value 
    if(visa_cat == "Visit")
        document.getElementById("new_textbox").style.display="block";
    else
        document.getElementById("new_textbox").style.display="none";
}
 </script>

In html form add 以html形式添加

<input type="text" name="new_textbox" id="new_textbox" style="display:none;">

and change 并改变

<select name="visa_categeory" id="visa_categeory" onChange="visacatOnchange();">

Good luck :-) 祝好运 :-)

Initially you can keep the textbox div hidden and then display it on "onchange" event of select input. 最初,您可以使文本框div隐藏,然后在选择输入的“ onchange”事件中显示它。

<div id="textboxdiv" style="visibility: hidden">
Visa Option <input type="text" id="someid"/>
</div>


 <select name="visa_categeory" id="visa_categeory"  onchange="showTextBox()" > 
         <option>Visit</option>
            <option>Business</option>
            <option value="99" >Other</option>
      </select>

Using Jquery you can display it like this:- 使用Jquery可以这样显示它:

      function showTextBox(){
      if ($('#visa_categeory').val() == '99') {
      $('#textboxdiv').css({'visibility':'visible'});
    }

Here's some basic JavaScript/jQuery that should help you solve your first problem: http://jsfiddle.net/j4aXQ/ 以下是一些基本的JavaScript / jQuery,可以帮助您解决第一个问题: http : //jsfiddle.net/j4aXQ/

HTML 的HTML

<form>
    <select name="visa_category">
        <option>Visit</option>
        <option>Business</option>
        <option>Other</option>
    </select>
</form>

JS JS

$('select').change(function() {
    var $selected = $('select option:selected');
    if ('Other' === $selected.text()) {
        $('form').append($('<input>').prop({'type':'text','id':'visa_other','name':'visa_other'}));
        $('form').append($('<label>').prop({'for':'visa_other'}).html('Testing'));
    } else {
        $('input').remove();
        $('label').remove();
    }
});

The above code will only display a text box (and accompanying label) if the "Other" option is selected, and will then remove that text box if a different option is subsequently picked. 如果选择了“其他”选项,则上面的代码将仅显示一个文本框(和附带的标签),如果随后选择了其他选项,则将删除该文本框。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM