隐藏字段值未显示在查询字符串中

Hidden field value is not appearing in query string

本文关键字:查询 字符串 显示 字段 隐藏      更新时间:2023-09-26

我有一个javascript方法,它正在做一些计算。最后,我想将其存储在隐藏字段中。这是方法:

<script>
function getQuantity(){
 var count = document.getElementById('hidden').value;
 var Quantity=new Array();
 var i=0;
 for(i=0; i<count; i++)
 {
    Quantity[i]=document.getElementById(i).value;
 }
var myJSONQuantity = JSON.stringify(Quantity,'');
alert(myJSONQuantity);
document.qForm.getElementById('hdnQuantityArray').value = myJSONQuantity;
alert(document.qForm.getElementById('hdnQuantityArray').value);
}
</script>

我已经检查了这里,一切正常。最后一个警报是打印正确的值。这是表单,其中有一个按钮,我想通过方法 GET 提交字段:

<form  name="qForm" method="GET" action="CalculateTotal.php">
    <input type="hidden" id="hdnQuantityArray" name="hdnQuantityArray">
    <input type="submit" value="CheckOut" id="CheckOut" name="CheckOut">
</form>

我的查询字符串显示:

http://localhost/blazorange/Customer/CalculateTotal.php?hdnQuantityArray=&CheckOut=Submit

我希望这个hdnQuantityArray在查询字符串中有一些值。

附言这个javascript方法是从同一文件中另一个表单的提交按钮调用的。这个形式是一个PHP形式。以下是其他表单的提交按钮的代码:

<input type="submit" value=" Total " id="total" onClick="return getQuantity()">

编辑:

<table border=1>
    <form id="CartForm">    
        <tr>
            <td>
                <h2> <font color='Grey'>Item Name</font> </h2>
            </td>
            <td>
                        <h2> <font color='Grey'>Item Price</font> </h2>
            </td>
            <td>
                    <h2> <font color='Grey'>Quantity</font> </h2>
            </td>
        </tr>
    <?PHP
        /*     Displaying the total and purchased cart's items    */
            $j=0;
            $temp=new Item();
            while(isset($ItemsArray[$j])) {
        ?>
        <tr>
                        <?PHP
                    if (is_string($ItemsArray[$j])) {
                $temp=unserialize($ItemsArray[$j]);
                 }
                $ItemName=$temp->getItemName();
                $Price=$temp->getPrice();
            ?>
            <td>
                <font color='Black'><?PHP echo $ItemName; ?>
            </td>
            <td>
                <font color='Black'><?PHP echo $Price;  ?></font>
            </td>
            <td>
                <input type="text" id="<?PHP echo $j;?>"/> 
            </td>
            </tr>

        <?PHP $j++; }?>

        <table>
            <input type="button" value=" Total " id="total" onClick="getQuantity()">
            <input type="hidden" value="<?PHP echo $j; ?>" id="hidden">
        </form>
    </table>

document.getElementById('hidden')

应该是

document.getElementById('hdnQuantityArray')

您发布的代码中没有 id 为"隐藏"的元素。

编辑:

你是说JS仅在提交CartForm时运行?这会重新加载页面,对吧?当页面重新加载时,hdnQuantityArray 为空。因此,当您提交 qForm 时,那里什么都没有,因为页面加载了一个空白字段,并且 JS 没有再次运行。听起来对吗?

你不应该这样做吗:

document.getElementById('hdnQuantityArray').value = myJSONQuantity;
alert(document.getElementById('hdnQuantityArray').value);

我删除了document.getElementById调用中不必要的"qform"。