简体   繁体   English

jQuery-选择框更改时URL不更新

[英]Jquery - URL not updating on select box change

I have a Ajax query that changes content based on a drop down. 我有一个基于下拉列表更改内容的Ajax查询。 The query works as it should but I cannot get it to update the URL with the select. 该查询按应有的方式工作,但是我无法通过选择来更新它。 If I select an item the content displays as it should but no URL change. 如果我选择一个项目,内容将按原样显示,但不会更改URL。 If I enter the URL manually it shows the content as it should. 如果我手动输入URL,它将按原样显示内容。

Am I missing something to get it to change the URL? 我是否缺少某些东西来更改URL?

$(document).ready(function(){
if($('.ID :selected').val()) {$('.ID').show();} else {$('.ID').hide();}  ;

$('.Name').change(function(e){
e.preventDefault();     
   var myval = $('.Name:selected').val();
   var baseurl = window.location.origin+window.location.pathname+"?ID="+myval;
    $.ajax(baseurl)
        .done(function (response) {
            $('body').html(response);
            $('.ID').show();
        })
        .fail (function (xhr) {
            alert('Error: ' + xhr.responseText);
            $('.ID').hide();
    });    
 });    
});

EDIT - I am using Silverstripe CMS. 编辑-我正在使用Silverstripe CMS。 I have an SQL query that populates from the DB 我有一个从数据库填充的SQL查询

$form = Form::create(
    $this,'MySearchForm',
    FieldList::create(
    DropdownField::create('ID',' ')
        ->setSource(TableName::get()->map('ID','MainName'))
        ->setemptyString('--Select Level --')
        ->addExtraClass('Name')
        ),

    FieldList::create());

    $form->setFormMethod('GET')
    ->setFormAction($this->Link()."#programme")
    ->disableSecurityToken()
    ->loadDataFrom($this->request->getVars());

    if($request->isAjax()) {
      $this->renderWith('Search');
    }

The problem is in your code, while getting the value syntax, I believe there is no need to use :selected with the val() . 问题出在您的代码中,在获取值语法的同时,我相信不需要与val()一起使用:selected You can use $('.ID').val() instead of $('.ID :selected').val() 您可以使用$('.ID').val()代替$('.ID :selected').val()

In addition to use this :selected you should follow this syntax $('.ID option:selected') 除了使用:selected ,还应遵循以下语法$('.ID option:selected')

Hope This might be helpfull for you !! 希望这可能对您有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM