简体   繁体   English

调用输入类型文本的函数-Javascript / PHP

[英]Function that call input type text - Javascript / PHP

i'm creating a form page where i 'ill put some inputs to my customers fill it. 我正在创建一个表单页面,我将在其中输入一些信息给我的客户。

but , every time i must write for example > 但是,每次我必须写例如>

<input type="number" class="form-control" name="quantidade" required max="900" />

for example , but i see a friend that was using javascript function to call theses input , so when he want insert a input like this , he just call on your index.page . 例如,但是我看到一个朋友正在使用javascript函数来调用这些输入,因此当他想要插入这样的输入时,他只是在index.page上调用。

someone could help ? 有人可以帮忙吗?

i have tryed this . 我已经尝试过了。

<script type="text/javascript">
  function add() {
    document.getElementById("inputnumber").innerHTML = '<input type="number" class="form-control" name="quantidade" required max="900" />';
  }
</script>

with this index.page 与此index.page

<div class="row">
    <div class="col-md-12">
        <div class="row">
            <div class="col-md-6">
                <form role="form">
                    <div class="form-group" id="inputnumber">
                    </div>
                    <div class="form-group">
                    </div>
                    <button type="submit" class="btn btn-default">
                        Submit
                    </button>
                </form>
            </div>
            <div class="col-md-6">
            </div>
        </div>
    </div>
</div>

yes this will work but you should place your function call after declaring your html element you're using. 是的,这可以工作,但是您应该在声明要使用的html元素之后放置函数调用。 for example 例如

<script type="text/javascript">
 function add()
 {
document.getElementById("inputnumber").innerHTML = '<input type="number"      class="form-control" name="quantidade" required max="900" />';
}
</script>
<div id="inputnumber"></div>
<script>
    add()
</script>

As you might notice the function call is placed after the "inputnumber" dev 您可能会注意到,函数调用位于“ inputnumber” dev之后

<div id="inputnumber"></div>

Here simple example: 这里简单的例子:

<script type="text/javascript">
    function add(){
        document.getElementById("inputnumber").innerHTML = '<input type="number" class="form-control" name="quantidade" required max="900"/>';
    }
</script>
<form role="form">
    <div class="form-group" id="inputnumber">
    </div>
    <button onclick="add();">Add +</button>
    <button type="submit" class="btn btn-default">
        Submit
    </button>
</form>

Note: If you want to add only one input element. 注意:如果只想添加一个输入元素。 Run this code and you will get it. 运行此代码,您将得到它。

For multiple element on your form, take a look at 对于表单上的多个元素 ,请查看

Dynamically add form element using Javascript-Simple one 使用Javascript动态添加表单元素-简单的一种
Append Element 追加元素
Good and Simple: Add Input Fields Dynamically to Form Using JavaScript 简单好用:使用JavaScript将输入字段动态添加到表单

So easy, u need this function, look: 很简单,您需要此功能,请看:

function callInput(a,b,c) {
    var x = document.createElement('input');
    x.className = 'form-control';
    x.type = b;
    x.name = c;
    a.appendChild(x);
}

How to use, look: 使用方法,外观:

callInput(parentElemen,'type','name');

Example: 例:

callInput(document.getElementsByTagName('form')[0],'submit','quantidade');

Good luck. 祝好运。

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

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