【问题标题】:How to pass a value from JQuery to servlet如何将值从 JQuery 传递到 servlet
【发布时间】:2015-05-03 04:49:52
【问题描述】:

这是我的脚本:

<script>
 $('#myModal').on('show.bs.modal', function(e) {

var metrics_key = $('.createAlarm').val();
var metrics_label = $(".createAlarm option:selected").text();

// Now you have the key and label in variables.

// Write the key into the textfield.
$('#myModal input[name="name"]').val(metrics_key);
// Change the HTML of an element to the label.
$('#myModal label[for="priority"]').html(metrics_label);
});

   var val = $('#priority').text();

   $.post("/<pjtname>/createAlarm", { val : "val"});
</script>

我在一个 JSP 页面中有一个 createAlarm 类,然后单击该页面中的一个按钮,会弹出以下模式:

 <!-- Modal- Create Alarm -->
  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
   <div class="modal-content">
    <div class="modal-header">
       <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
       <h4 class="modal-title">Create Alarm</h4>
     </div>
    <form action="/CMPE283_Project2/createAlarm" method="post" id="addcard" class="form-horizontal" role="form">
   <div class="modal-body" style="height: 170px;">
       <div class="form-group" style="height: 30px;">
            <label for="title" class="col-md-3 control-label">Name</label>
            <div class="col-md-9">
               <input type="text" class="form-control" name="alarmName">
            </div>
        </div>
         <div class="form-group" style="height: 30px; margin-bottom:30px;">
            <label for="title" class="col-md-3 control-label">Description</label>
            <div class="col-md-9">
               <textarea class="form-control" name="desc"></textarea>
            </div>
        </div>

           <div class="form-group">
            <label for="priority" id="priority" name="priority" class="col-md-3 control-label"> </label>
            <div class="col-md-9">
                <div class="col-md-3">
                <select name="metric" class="form-control">
                <option value="lessthan"><</option>
                <option value="greaterthan">></option>
                <option value="lessthanequalto"><=</option>
                <option value="greaterthanequalto">>=</option>
                </select>
                </div>
                <div class="col-md-3">
                    <input type="text" class="form-control" name="metricnum">
                </div>
               </div>
            </div>
           </div>

我想将优先级标签的值传递给 servlet 页面。我试过了:

String val = request.getParameter("val");

但是,我得到的是空值。请帮忙。谢谢。

【问题讨论】:

  • 您有多个 id=priority 的字段吗?
  • 在提交之前尝试做alert($('#priority').text())。你得到了什么价值?
  • alert 正在显示我想传递给 servlet 的正确值。
  • 试试这个,它应该可以工作$.post("/&lt;pjtname&gt;/createAlarm", { val : $("#priority").text()});
  • 这不起作用,仍然将空值传递给 servlet。 :(

标签: java javascript jquery jsp servlets


【解决方案1】:

$('#priority').text() 从 html 内容中获取值。首先在 Jquery 中使用 post 或 get 方法将值提交给 servlet。下面的链接帮助使用 get / post 方法。 http://www.w3schools.com/jquery/jquery_ajax_get_post.asp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    相关资源
    最近更新 更多