0

我正在尝试实现一个绑定到选择框的 jQuery 滑块,作为我正在整理的问卷的一部分。但是,我在 Chrome 中收到Uncaught SyntaxError并在 IE中收到预期的 ')'错误。

我正在使用的 jQuery 如下,是来自 jQuery UI 站点的直接副本

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">
  $(function() {
    var select = $( ".minbeds" );
    var slider = $( "<div id="slider"></div>" ).insertAfter( select ).slider({
      min: 1,
      max: 4,
      range: "min",
      value: select[ 0 ].selectedIndex + 1,
      slide: function( event, ui ) {
        select[ 0 ].selectedIndex = ui.value - 1;
      }
    });

    $( ".minbeds" ).change(function() {
      slider.slider( "value", this.selectedIndex + 1 );
    });
  });
</script>

我对代码所做的唯一更改是我将#minbeds 从和ID 更改为一个类(.minbeds)。

HTML如下:

    <select class="minbeds">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
    </select>

现在,代码似乎工作正常,在我的本地机器上没有错误,但是一旦我将它放入 CMS,我就会在以下行之间收到Uncaught SyntaxError: Unexpected Identifier错误(在 chrome 中):

var slider = $( "<div id="slider"></div>" ).insertAfter( select ).slider({

min: 1,

在 IE9 中,我得到Expected ')'星号在此处指示的位置:

var slider = $( "<div id="*slider"></div>" ).insertAfter( select ).slider({

有谁知道为什么会发生这种情况?关于如何缩小错误范围的任何想法?我试过使用 JSLint,但我不知道如何正确使用它。

如果有帮助,这里是 CMS 的完整源代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head"><title>
test | Gael Ltd
</title><meta charset="UTF-8" /> 
<link href="/CMSPages/GetCSS.aspx?stylesheetname=GaelCorporate" type="text/css" rel="stylesheet" /> 
<link href="/App_Themes/Default/DesignMode.css" type="text/css" rel="stylesheet" /> 
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js" type="text/javascript"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" type="text/javascript"></script>
<script type="text/javascript">
  $(function() {

    var select = $( ".minbeds" );
    var slider = $( "<div id="slider"></div>" ).insertAfter( select ).slider({
      min: 1,
      max: 4,
      range: "min",
      value: select[ 0 ].selectedIndex + 1,
      slide: function( event, ui ) {
        select[ 0 ].selectedIndex = ui.value - 1;
      }
    });
    $( ".minbeds" ).change(function() {
      slider.slider( "value", this.selectedIndex + 1 );
    });
  });
</script> 
<link href="/favicon.ico" type="image/x-icon" rel="shortcut icon" /> 
<link href="/favicon.ico" type="image/x-icon" rel="icon" /> 
<link href="/App_Themes/GaelCorporate/site_superfish.css" type="text/css" rel="stylesheet" />
<link href="/App_Themes/GaelCorporate/superfish.css" type="text/css" rel="stylesheet" />
</head>

<body class="LTR IE IE9 ENGB ContentBody">
<form method="post" action="/test-folder/test.aspx" id="form">

<div class="aspNetHidden">
  <input type="hidden" name="manScript_HiddenField" id="manScript_HiddenField" value="" />
  <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
  <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
</div>

<script type="text/javascript">
//<![CDATA[
  var theForm = document.forms['form'];
  if (!theForm) {
      theForm = document.form;
  }
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>


<script src="/WebResource.axd?d=jPoMVS0EaQHOPmIi_xm2HU7yLiOSI9SwD_nAvxdPGg3slCV5HIxf7PSCa8BfM4b35NfjJ4LD1KDvV9zGMhi-uyAc4R1j13kmLSk37dPSwRA1&amp;t=635080272185083230" type="text/javascript"></script>

<input type="hidden" name="lng" id="lng" value="en-GB" /><div>
<input type="hidden" name="vmode" id="vmode" value="2" />
</div>

<script src="/CMSPages/GetResource.ashx?scriptfile=%7e%2fCMSScripts%2fcmsedit.js" type="text/javascript"></script>
<script src="/ScriptResource.axd?d=qITU4oF4GWOpdURxveOn4klAk_JXip46ZHYKRqOUaPkNBV9OLAT0zMmPuZjF0ltSZKQlH5lCAk6I-L0CB4BfF84f5lbs5uiDmHRHcZEufPBw7X-M84RuYSjEXgjwE-gx0&amp;t=34d147fd" type="text/javascript"></script>
<script src="/ScriptResource.axd?d=05xl_ew97J9D2HSa4Uccwf9-hvRXKd018osO5VbhpBA71UbuP-EbmT7rx4i0SCEzVoF8fX63gNR8y-mVT88Ca0QEYuqmsHW4GGpDjGvljtfyIoQXInytcQQGxUI7XAD5x4baeNOY1cVoynUnafwICw2&amp;t=34d147fd" type="text/javascript"></script>

<div class="aspNetHidden">
<input type="hidden" name="__SCROLLPOSITIONX" id="__SCROLLPOSITIONX" value="0" />
<input type="hidden" name="__SCROLLPOSITIONY" id="__SCROLLPOSITIONY" value="0" />
</div>

<script type="text/javascript">
//<![CDATA[
  Sys.WebForms.PageRequestManager._initialize('manScript', 'form', [], [], [], 90, '');
//]]>
</script>

<div id="m" style="background:none;">
<div id="CMSHeaderDiv">
    <div id="m_PageManagerInfoContainer" class="PageManagerInfoContainer">
        <!-- --><div id="m_pnlPreviewInfo" class="PageManagerPreviewInfo">
            <span class="PreviewInfo">This is a preview mode of document <strong>test</strong>.</span><a class="PreviewInfoLink" href="javascript:__doPostBack(&#39;m$ctl02&#39;,&#39;&#39;)">Close</a><span class="PreviewInfo">the preview mode.</span>
            </div>
    </div>

<script type="text/javascript">
    //<![CDATA[
    if ( (parent != null) && (parent.IsCMSDesk) ) { infoElem = document.getElementById('m_pnlPreviewInfo'); if (infoElem) {if ( infoElem.style ) { infoElem.style.display = 'none'; } else { infoElem.display = 'none'; } }}
//]]>
</script><!-- -->
</div>
</div>

<div id="pagewrapper">
  <!-- vertical_menu div start -->
<div id="vertical_menu">


</div><!-- vertical_menu div end -->

<!-- content_container div start -->
<div id="content_container">
<div id="zoneContent">
    <div id="p_lt_zoneContent_BizForm_viewBiz">
<div id="p_lt_zoneContent_BizForm_viewBiz_ctl00">
    <div id="p_lt_zoneContent_BizForm_viewBiz_ctl00_pnlForm" class="FormPanel" onkeypress="javascript:return WebForm_FireDefaultButton(event, &#39;p_lt_zoneContent_BizForm_viewBiz_ctl00_btnOK&#39;)">
        <table class="EditingFormTable"><tr><td class="EditingFormLabelCell"><label id="p_lt_zoneContent_BizForm_viewBiz_ctl00_select_lb" class="EditingFormLabel" for="p_lt_zoneContent_BizForm_viewBiz_ctl00_select_dropDownList">select field</label></td><td class="EditingFormValueCell"><div class="EditingFormControlNestedControl">

<select name="p$lt$zoneContent$BizForm$viewBiz$ctl00$select$dropDownList" id="p_lt_zoneContent_BizForm_viewBiz_ctl00_select_dropDownList" class="minbeds">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
</select>

</div></td></tr><tr>

<td class="EditingFormButtonLeftCell"></td>
<td class="EditingFormButtonCell">
    <input type="submit" name="p$lt$zoneContent$BizForm$viewBiz$ctl00$btnOK" value="OK" id="p_lt_zoneContent_BizForm_viewBiz_ctl00_btnOK" class="FormButton" />
  </td></tr></table>
  </div>
</div>
</div>

<select name="minbeds" class="selectInput">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
</select>
</div>

<div class="bottomWebParts"></div>

</div>
<!-- content_container div end -->

<!-- primary webparts div start -->
<div id="primary_webparts"></div>

<!-- primary webparts div end -->
</div>

<script type="text/javascript">
//<![CDATA[
  (function() {var fn = function() {$get("manScript_HiddenField").value = '';Sys.Application.remove_init(fn);};Sys.Application.add_init(fn);})();//]]>
</script>

<script type="text/javascript">
  //<![CDATA[
   InitializePage();
  //]]>
</script>

<script type="text/javascript">
  //<![CDATA[
    theForm.oldSubmit = theForm.submit;
    theForm.submit = WebForm_SaveScrollPositionSubmit;

    theForm.oldOnSubmit = theForm.onsubmit;
    theForm.onsubmit = WebForm_SaveScrollPositionOnSubmit;
  //]]>
</script>

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTYwODg0MzQ2Ng9kFgICARBkZBYCAgMPZBYCZg9kFgJmD2QWAgIDD2QWAmYPZBYCZg8PFgIeCEZvcm1Nb2RlCyl1Q01TLkZvcm1FbmdpbmUuRm9ybU1vZGVFbnVtLCBDTVMuRm9ybUVuZ2luZSwgVmVyc2lvbj02LjAuNDcxMy4yNzkyNiwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj04MzRiMTJhMjU4ZjIxM2Y5AGQWAgICD2QWAmYPZBYEAgEPDxYCHgdWaXNpYmxlaGQWAgIBDw8WBB4IQ3NzQ2xhc3MFH0VkaXRpbmdGb3JtQ29udHJvbE5lc3RlZENvbnRyb2weBF8hU0ICAmQWAmYPZBYCZg8PFgQfAgUKTGFiZWxGaWVsZB8DAgJkZAIHD2QWAgIBDw8WBB8CBR9FZGl0aW5nRm9ybUNvbnRyb2xOZXN0ZWRDb250cm9sHwMCAmQWAmYPDxYCHwJkZBYCZg8QDxYEHwIFB21pbmJlZHMfAwICZA8WBGYCAQICAgMWBBAFATEFATFnEAUBMgUBMmcQBQEzBQEzZxAFATQFATRnZGRkpyri0ouEXB8y3l0reZFw/5xfESlKtjr/PsCd4HT+bG4=" />
<script type="text/javascript"> 
  //<![CDATA[
  if (window.WebForm_InitCallback) { 
    __theFormPostData = '';
    __theFormPostCollection = new Array();
    window.WebForm_InitCallback(); 
  }
  //]]>
</script>
</form>

</body>
</html>
4

2 回答 2

2

试试喜欢

var slider = $( "<div id='*slider'></div>" ).insertAfter( select ).slider({

或者直接你可以这样做

var slider = $( "div #slider" ).insertAfter( select ).slider({

我认为你需要加上select单引号或双引号。

于 2013-07-31T09:07:38.947 回答
1

尝试这个

var slider = $( '<div id="slider"></div>' ).insertAfter( select ).slider({
于 2013-07-31T09:09:51.493 回答