使用JQuery和bootstrap更改选择选项的标签文本

时间:2014-12-08 22:44:22

标签: javascript jquery html twitter-bootstrap twitter-bootstrap-3

我正在使用bootstrap 3和jQuery 1.11处理模态。模态具有选择,标签,输入和字形元素。我想根据所选的选项更改标签内容和字符串的标题。我写了以下代码来实现这个

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <div class="modal fade" id="apstStopModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog large">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">Enter  Information</h4>
          </div>
          <form class="form-horizontal" id="stopForm">
            <div class="modal-body">
              <fieldset class="frame-border">
                <legend class="frame-border">Start-up Time</legend>
                <div class="col-xs-12 col-md-2">
                  <div class="form-group">
                    <div class="controls col-xs-12 col-md-12">
                      <span class="help glyphicon glyphicon-info-sign" data-toggle="popover" data-placement="right" title="Start-up time of the stop."></span>
                      <select id="startUpTime" name="StartUpTime" class="form-control" >
                        <option value="Fixed">Fixed</option>
                        <option value="Uniform">Uniform</option>
                        <option value="Truncated Normal">Truncated Normal</option>
                        <option value="Exponential">Exponential</option>
                      </select>
                    </div>
                  </div>
                </div>
                <div class="col-xs-12 col-md-5">
                  <div class="form-group required">
                    <label class="control-label col-xs-6 col-md-6" for="startUpTimeParam1" id="startUpParam1Label" >Name</label>
                    <div class="controls col-xs-6 col-md-6">
                      <span class="help glyphicon glyphicon-info-sign" id="startUpParam1Help" data-toggle="popover" data-placement="right" title="Help"></span>
                      <input type="number" id="startUpTimeParam1" name="Name" class="form-control" value="0.00">
                    </div>
                  </div>
                </div>
                <div class="col-xs-12 col-md-5">
                  <div class="form-group required">
                    <label class="control-label col-xs-6 col-md-6" for="startUpTimeParam2" id="startUpParam2Label" >Name</label>
                    <div class="controls col-xs-6 col-md-6">
                      <span class="help glyphicon glyphicon-info-sign" id="startUpParam2Help" data-toggle="popover" data-placement="right" title="Help"></span>
                      <input type="number" id="startUpTimeParam2" name="Name" class="form-control"  value="0.00">
                    </div>
                  </div>
                </div>
              </fieldset>
              <script src="ApstCustomJS.js"></script>
            </div>
            <!-- modal-body -->
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="submit" class="btn btn-primary">Save changes</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </body>
</html>

的JavaScript

function startUpChange() {
     var theValue = $("#startUpTime").find("option:selected").text();
     if(theValue === "Fixed" )
         {
         $('#startUpParam1Help').attr("title","Please provide the start time");
         $('#startUpParam1Label').html("Start Time");
         $('#startUpParam2Help').attr("title","Please provide the stop time");
         $('#startUpParam2Label').html("Stop Time");
         }
     else if(theValue === "Uniform" )
     {
     $('#startUpParam1Help').attr("title","Please provide the lower bound time");
     $('#startUpParam1Label').html("Lower Bound");
     $('#startUpParam2Help').attr("title","Please provide the upper bound time");
     $('#startUpParam2Label').html("Upper Bound");
     }
     else if(theValue === "Turncated Normal" )
     {
     $('#startUpParam1Help').attr("title","Please provide the mean time ");
     $('#startUpParam1Label').html("Mean");
     $('#startUpParam2Help').attr("title","Please provide the standard deviation time");
     $('#startUpParam2Label').html("Standard Deviation");
     }
}

$("#startUpTime").change(startUpChange()).change();

CSS

.form-group {
 padding-right:20px;
  position:relative;
}

 .help {
 position:absolute;
 right:-8px;
 top:12px;
}

fieldset.frame-border {
    border: 1px groove #ddd !important;
    padding: 0 1em !important;
    margin: 0 0 1em 0 !important;
    -webkit-box-shadow:  0px 0px 0px 0px #000;
            box-shadow:  0px 0px 0px 0px #000;
}

legend.frame-border {
    font-size: 1.1em !important;
    font-weight: bold !important;
    text-align: left !important;
    width:inherit;
    padding:0 10px;
    border-bottom:none;
}

.form-group.required .control-label:after { 
   content:"*";
   color:red;
}

以下是它现在的样子。

current

Javascript无法正常工作,因为标签和popover标题在第一次加载页面时会更改,但在加载后选择其他选项时不会更改。我想确保它的工作方式类似于this。我是新的jQuery,Bootstrap和Web开发我认为它可能与bootstrap和jquery兼容性有关。我需要帮助修复它。

1 个答案:

答案 0 :(得分:1)

您的函数调用不正确,请将最后一行js代码更改为:

$("#startUpTime").change(function() {
  startUpChange();
});

您的js代码中也有拼写错误。 else if(theValue === "Turncated Normal" )截断写错了,因此除非您更改它,否则无法正常工作。

Working Example