0

在我的 jsp 页面中,我有一个 div,如下所示,我正在通过 javascript 事件设置它的值。

<div id="panel">Price
            <div id="field_name" name="priceval"></div>
        </div>

我在这里面临的问题是,我将 priceval 值设为 null

String price = request.getParameter("priceval");
int priceval = Integer.parseInt(price);

作为参考,这是我的整个 jsp 文件

<html>

<style media="screen" type="text/css">

#field_name { display:inline-block; }

</style>

<head>
<title>Online Book Store</title>
<script>



function myFunction() {

    var quantityvalue=document.getElementById("quan").value;
     var selObj = document.getElementById('selSeaShells');
     var optionselectedvalue ;
    // var optionselectedtext;

     for (i=0; i<selObj.options.length; i++) {
           if (selObj.options[i].selected) {
             optionselectedvalue = selObj.options[i].value.split(':')[0];

                }
              }

    var finalprice = quantityvalue*optionselectedvalue;


    mydiv = document.getElementById("panel");
    mydiv.style.display = "block"; //to show it
    document.getElementById("field_name").innerHTML = finalprice;

}

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    return !(charCode > 31 && (charCode < 48 || charCode > 57));
}

function hideDiv() {
    mydiv = document.getElementById("panel");
    mydiv.style.display = "none"; //to hide it  
}
window.onload = function () {
    hideDiv();
}

</script>
</head>
<Body Bgcolor = "wheat" onload="hideDiv()">
<center>
<h2> welcome to online shopping</h2>
<form Action = "./onlinebookstore">
select Book
 <select name = "book" id="selSeaShells">
 <option></option>
 <option value=  "100:C programming language">C programming language</option>
 <option value = "200:Java programming language">Java programming language</option>
 <option value = "300:Network analysis">Network analysis</option>
 <option value = "400:Advaced computer Networks">Advaced computer Networks</option> 
 <option value ="500:Programming analysis">Programming analysis</option>
  <option value = "600:Advanced data strctures">Advanced data strctures</option>
 <option value = "700:Advanced java and web technologies">Advanced java and web technologies</option>
 <option value = "800:Electronic devices">Electronic devices</option>
 <option value = "900:Computer Graphics">Computer Graphics</option>
 <option value = "1000:Microprocessors">Microprocessors</option>
 <option value = "1100:Oracle">Oracle</option>

  </select>
 <br><br>
  quantity <Input type = "text" name = "Quantity" id="quan" onkeypress="return isNumberKey(event);" onblur="myFunction()">
 <br><br>

 <div id="panel">Price
            <div id="field_name" name="priceval"></div>
        </div>

 <input type = "submit" name = "s" value = "ADDItem">
 <input type = "submit" name = "s" value = "Removeitem">
 <input type = "submit" name = "s" value = "showitems">
 <input type = "submit" value = "Logout" name = "s">
 </form>
</center>
</Body>
</html>
4

3 回答 3

1

div标签不是表单提交的一部分。您可以动态创建<input>可在提交中使用的标签。有关 div 标签,请参阅w3参考。

于 2013-07-09T22:29:23.477 回答
0

adiv不是参数,参数在 URL 上或来自 a form

于 2013-07-09T22:23:17.903 回答
0

与其他答案相同,div 不是要传递的参数...要传递计算价格的值,您可以使用隐藏输入并使用带有 div 的 javascript 设置它并在 servlet 中获取其值。

像那样

JSP

   <input type="hidden" name="price" id="price">

JavaScript

document.getElementById("price").value=calculatedPrice;

小服务程序

   price = request.getParameter("price");
于 2013-07-09T22:35:22.033 回答