【问题标题】:Unable to pass the jQuery value to servlet无法将 jQuery 值传递给 servlet
【发布时间】:2015-07-12 16:58:24
【问题描述】:

我想将一个变量从 jQuery 发送到我的 Servlet。这是我的 jQuery:

<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();

console.log(val);

$.post('/sampleapp/createAlarm', { val : val},
        function() { // on success
            alert("Insertion successful! of "+val);
    })
    .fail(function() { //on failure
        alert("Insertion failed." +val);
    });  

 });
 </script>

在 servlet 上,我正在使用:

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

请在下面找到 HTML 代码。当我从下拉列表中选择一个值并单击创建警报按钮时,会弹出引导模式。我想将所有这些值从弹出窗口传递给我的 servlet。除了我在 jQuery 中设置的值之外,我可以做所有这些。

    <div class="container" style="padding-top: 60px;" >                                          
       <select class="createAlarm" id="createAlarm" name="metrics">
        <option value="cpuUsage">CPU Usage</option>
        <option value="memoryUsage">Memory Usage</option>
        <option value="diskRead">Disk Read</option>
        <option value="diskWrite">Disk Write</option>
        <option value="diskUsage">Disk Usage</option>
        <option value="netUsage">Net Usage</option> 
       </select>
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" data-whatever="Create Alarm">Create Alarm</button>
   </div>

    <!-- 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">
            <input type="text" class="form-control" name="name">
             </label>
            <div class="col-md-9">
                <div class="col-md-3">
                <select name="sign" 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="threshold">
                </div>
               </div>
              </div>
             </div>
            <div class="modal-footer">
           <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
           <button class="btn btn-primary" id="formSubmit" type="submit">Create Alarm</button>
           </div>
          </form>
       </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
      </div><!-- /.modal -->

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

【问题讨论】:

  • #priority 由什么组成?显示 HTML 代码。
  • 不回答问题,但您应该使用 $('#myModal label[for="priority"]').text(metrics_label); (文本而不是 html),因为你得到了带有 text() 的 metrics_label。

标签: java jquery twitter-bootstrap servlets


【解决方案1】:

insead of post 尝试使用这种方式

$.ajax({
                url : url,
                type : "post",
                data : values,
                success : function(data) {
                    if (data.errorCode == "SUCCESS") {

                    } else {

                    }

                },
                error : function() {
                    alert("failure");

                }

你的值是表单还是 JSON 否则尝试 {"val" : val}

【讨论】:

  • 我用数据尝试了这个:{"val":val},仍然没有运气。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 2012-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多