按钮onClick的打印值

时间:2018-07-23 11:16:39

标签: javascript random draw

我无法输出按钮的值,例如我有一个按钮的值 是(1、2、3、4 ..... 12)。当我单击6个按钮示例时,值是(1,2,3,4,5,6)并单击 生成按钮按钮的值将在页面上输出。然后点击另一个 在换行符中输出6个按钮。并且不要重复相同的数字。

    function Print(num1) {
        btn.value = btn.value + num1;
    }
    <p id="demo"></p>
    <input type="button" name="btn" value="1">
    <input type="button" name="btn" value="2">
    <input type="button" name="btn" value="3">
    <input type="button" name="btn" value="4">
    <input type="button" name="btn" value="5">
    <input type="button" name="btn" value="6"><br>
    <input type="button" name="btn" value="7">
    <input type="button" name="btn" value="8">
    <input type="button" name="btn" value="9">
    <input type="button" name="btn" value="10">
    <input type="button" name="btn" value="11">
    <input type="button" name="btn" value="12"><br><br>
    <button onclick="document.getElementById(" demo").innerHTML=Print()">Generate</button>

2 个答案:

答案 0 :(得分:0)

您需要使用ajax函数,这是ID中的代码放置值

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://code.jquery.com/jquery-1.12.4.js">
   </script>
    <script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js'></script>

 <script>

$(document).ready(function(){
    $(".1button1").click(function(e){
        $.ajax({
            url: "attachment.php",
            type: "POST",
            data: {colID: e.target.id}, //this sends the user-id to php as a post variable, in php it can be accessed as $_POST['uid']
            success: function(data){
             alert(data);
              // data = JSON.parse(data);
             //  document.getElementById("demo").innerHTML =  data;
              // $('#datatable').DataTable({ 
              //        "destroy": true, //use for reinitialize datatable
              //     });
                //document.getElementById('myField').value= this.responseText;
                //update some fields with the updated data
                //you can access the data like 'data["driver"]'
            }
        });
    });
});

</script>


 <span class="1button1" id="1" onmouseover="" style="cursor: pointer;"/> Click Me</span>

然后进入attachment.php页面

if(isset($_POST['colID'])){

    $nm = $_POST['colID'];
    echo $nm ;

}

答案 1 :(得分:0)

这将为您提供帮助。

<div id="button-container">
<br>
</div>
<br>
<button onclick="Print(event)">Generate</button>
<script>
    function Print(e) {
        var container = document.getElementById('button-container'),
            children = container.children,
            lastValue = 0,
            button,
            lineBreak = document.createElement('br');
        if (children) {
            for (var i = 0; i <= children.length - 1; i++) {
                if (children[i].value) {
                    lastValue = parseInt(children[i].value);
                }
            }
        }
        var limit = lastValue + 6;
        for (var i = lastValue + 1; i <= limit; i++) {
            button = document.createElement('input');
            button.setAttribute('type', 'button');
            button.setAttribute('name', 'btn');
            button.setAttribute('value', i);
            container.appendChild(button);
        }
        container.appendChild(lineBreak);
    }
</script>