通过JavaScript更改值



按钮的值应该在1或0中改变,但echo $_POST["ordina"]总是给出1,我不明白,因为代码似乎是正确的。

<script> function order() {
if (document.ordination.ordina.value == "1") { 
    document.ordination.ordina.value = "0";
} else { 
    document.ordination.ordina.value = "1"; 
} }</script>
<?php echo $_POST["ordina"]; ?>
<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="1" class="button" name="ordina" onclick="order();return true;">Ordina</button>

alert(document.ordination.ordina.value)总是给出1。

有人能帮我吗?

现在检查一下…

<script> function order() {
if (document.ordination.ordina.value == "1") {
     alert(document.ordination.ordina.value);  // this one shows 1
    document.ordination.ordina.value = "0";
} else { 
    alert(document.ordination.ordina.value);  // this one shows 0
    document.ordination.ordina.value = "1"; 
} }</script>
<?php echo $_POST['ordina'];?>
<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="<?php if(isset($_POST['ordina'])){echo $_POST['ordina'];}else{ echo '1';}?>" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>

提交此表单后,<?php echo $_POST['ordina'];?>为0…并将按钮值设置为0,再次提交该值可更改为1。

和它的结果改变了0到1到0到1但是你是第一次加载这个页面意味着警报只显示1 .

这总是返回1,因为你的按钮是一个"提交"按钮,所以每次你点击按钮时,body都会重新加载。

    <script>
    function order() {
    if (document.ordination.ordina.value == "1") { 
        document.ordination.ordina.value = "0";
    } else { 
        document.ordination.ordina.value = "1"; 
    }
    alert(document.ordination.ordina.value);
    }
    </script>
    <form id="ordination" name="ordination" method="POST" action="">
    <button type="button" value="1" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>
    </form>

我将button的type改为submit,然后它就可以工作了

<script> function order() {
if ($("#ordina").val() == "1") { 
    $("#ordina").val(0);
} else { 
    $("#ordina").val(1); 
} }</script>
<?php echo $_POST["ordina"]; ?>
<form id="ordination" name="ordination" method="POST" action="">
<button type="submit" value="1" class="button" name="ordina" id="ordina" onclick="order();return true;">Ordina</button>

相关内容

  • 没有找到相关文章

最新更新