【问题标题】:change value of dynamically created buttons更改动态创建的按钮的值
【发布时间】:2017-04-01 17:53:46
【问题描述】:

我想将值分配给 0,如果它为 1,反之亦然,然后单击按钮,然后将其保存到数组中,根据查询可能有 20-25 个按钮

<?php while($row = mysqli_fetch_array($result)) { ?>

    <button  class="btn-success btn-sq btn-lg " value="0" id="<?php echo $row[0]; ?>" onclick="toggle(this) ">
        <?php echo $row[0]; ?> <br>
    </button>

<?php } ?>

【问题讨论】:

  • 这些 1 和 0 是从哪里来的? $row[0]?

标签: javascript php html twitter-bootstrap


【解决方案1】:

如果您只是想切换值,这里有一个简单的 jsfiddle 可以做到这一点:https://jsfiddle.net/fz1o0dsr/

$(document).ready(function(){
    $("button").click(function(){
    var $this = $(this);
    $this.attr('value', $this.attr('value') === "0" ? "1" : "0");
    alert("The button val is now " + $this.attr('value'));
  });
});

【讨论】:

  • thanx 得到了一个简单的解决方案
【解决方案2】:

感谢这对我有用的帮助

<script>
    function toggle(a)
{
    if(a.value==1)
    {
        a.value=0;   
    }
    else
    {
        a.value=1;
    }

}

【讨论】:

    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    相关资源
    最近更新 更多