简体   繁体   English

如何仅使用JS和HTML创建按钮

[英]How to create a button using only JS and HTML

I was asked to create a very very basic POS after some the user selects what the what to buy and the quantity, it should show on a table row showing price, quantity and product; 我被要求创建一个非常非常基本的POS后,一些用户选择了什么购买和数量,它应显示在显示价格,数量和产品的表格行; i´ve done that already but i was also asked to make a button next to the row that could eliminate the whole row if the user wants to. 我已经这样做了但是我还被要求在行旁边做一个按钮,如果用户愿意,可以消除整行。 Here is my JS code: 这是我的JS代码:

function histrow() {

    var prod = document.getElementById("product");
    var cant = document.getElementById("quantity");
    var pre = document.getElementById("prrice");
    var table = document.getElementById("tblHistory");
    var posicion = table.rows.length;
    var newRow = table.insertRow(posicion);

    //Creates the new cells
    var celUno = newRow.insertCell(0);
    var celDos = newRow.insertCell(1);
    var celTres = newRow.insertCell(2);
    var celCuatro = newRow.insertCell(3);
    var celCinco = newRow.insertCell(4);

    //Starts to asign values to the cells 
    celUno.innerHTML = prod.value;
    celDos.innerHTML = cant.value;
    celTres.innerHTML = pre.value;
    //This is the TOTAL
    celCuatro.innerHTML = pre.value * cant.value;
    //Here is where I want the button to show
    celCinco.innerHTML = 


}

I can't create just a button on HTML because I don't how many rows will the user create, my teacher is will only allow us to use HTML and JavaScript It's my first semester in software engineering Thanks for the help 我无法在HTML上创建一个按钮,因为我没有用户创建多少行,我的老师只允许我们使用HTML和JavaScript这是我在软件工程的第一个学期感谢您的帮助

 <!-- App4 references -->
<link href="/css/default.css" rel="stylesheet" />
<script src="/js/default.js"></script>

<div id="part1">
    <center>
            <h2> POS </h2>
            <br />
            <br />
            <select class="venta" id="product">
                <option>Choose a product</option>
                <option id="1" >1</option>
                <option id="2" >2</option>
                <option id="3" >3</option>
                <option id="4" >4</option>
                <option id="5" >5</option>
                <option id="6" >6</option>
                <option id="7" >7</option>
                <!--Hasta aqui van las bebidas-->
                <option id="8" value="Cheetos">Cheetos</option>
                <option id="9">9</option>
                <option id="10">10</option>
                <option id="11">11</option>
                <option id="12">12</option>
                <option id="13">13</option>
                <option id="14">14</option>
                <option id="15">15</option>
            </select>
            <label> Product</label>
            <br />
                <!--Es el precio del producto-->
            <input type="number" id="prrice" placeholder="¿How much?" class="sale" />
            <label> Price</label>
            <br />
                 <!--Its the quantity of the product-->
            <input type="number" min="1" id="quantity" placeholder="¿How many?" class="sale" />
            <label> Quantity</label>
            <br />
            <br />
                <!--This button adds the order to the table -->
            <button id="btnadd">Add to order </button>

    </center>
    </div>

<!--Here is the table where it shows what the user orders-->

<div id="part2">
    <table id="tblHistory>
        <thead>
            <tr>
                <td>Product</td>
                <td>Quantity</td>
                <td>Price</td>
                <td>Total</td>
            </tr>
        </thead>
        <tbody></tbody>
    </table>
</div>

This is what you want, i added a button to the fiddle so you have an event to see it work: https://jsfiddle.net/0j1me71m/3/ 这就是你想要的,我给小提琴添加了一个按钮,所以你有一个事件让它看起来有用: https//jsfiddle.net/0j1me71m/3/

    function editTextArea() {
       var options = document.getElementById("options");
       options.innerHTML = options.innerHTML + "<button type='button' onclick='doSomething()'>I am a Button</button><br>";
    }
    function doSomething(){
       alert("see me work!");
    }

Here is your answer: 这是你的答案:

http://liveweave.com/gGxK4b http://liveweave.com/gGxK4b

JavaScript: JavaScript的:

// Create the button and the text inside
var btn = document.createElement("button");
var btnText = document.createTextNode("This is just a button");
btn.appendChild(btnText);

// Add to the div (main)
var element = document.getElementById("main");
element.appendChild(btn);

HTML: HTML:

<div id="main">
</div>

What you're trying to do is suboptimal. 你想要做的是不是最理想的。

Instead, have your HTML button defined in a variable and inject it to the page when needed. 相反,将HTML按钮定义在变量中,并在需要时将其注入页面。

Basically something like this: 基本上是这样的:

<script>
var button = "<button style=\"background-color:green; border:1px solid darkgreen; color:white; font-weight:bold; padding: 5px 10px;\">PURCHASE</button>";

document.getElementById('tag-id').innerHTML = button;
</script>

See Demo 见演示

暂无
暂无

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

相关问题 如何<a>使用 JS</a>创建 Bulma 按钮元素而不是 HTML - How to create Bulma button element rather than HTML <a>, using JS 如何使用 JS 或 jQuery 仅创建 HTML 元素的起始标签? - How to create only a starting tag of an HTML element using JS or jQuery? 如何创建重试连接按钮? HTML / JS - How to create retry connecting button? HTML/JS 如何使用 Vanilla JS HTML CSS 仅隐藏属于同一类按钮的单击按钮 - How to hide only clicked button belonging to same class of buttons using Vanilla JS HTML CSS 如何仅使用javascript创建单选按钮(我不想使用html标签)? - How to create a radio button using only javascript (I don't want to use html tags)? 如何使用HTML和JS在foreach循环中为单个按钮创建动态ID - How to create dynamic id to a single button in foreach loop using html & js 如何在 Angulas JS 中创建按钮并在 HTML 页面中使用 ng-repeat 显示它们 - How to create button in Angulas JS and display them using ng-repeat in the HTML page 如何使用 HTML 和 CSS 创建可切换按钮 - How to Create Toggleable Button using HTML and CSS 如何使用 html 和 javascript 创建复制按钮? - How to create copy button using html and javascript? 如何在不使用数据库的情况下创建像 facebook 这样的简单帖子并将其堆叠在一起,仅使用 HTML、CSS 和 JS? - How to create a simple post like facebook without using a database and stack it together, With only HTML, CSS, & JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM