1

This is my snippet code here.I'm getting some column values from shrepoint list to a dropdpwnlist.But i would like to append the values to a url when an option is selected. So thayt i can use it to filter a list as a parameter. The problem i have is after i select an option and the pages url changes and appends a value to the URL the value i selected isn't set to what i selected but goes back to "select" the default value.

<select id="Phones">

<option value="Select">select</option>
<option value="Alcatel">Alcatel</option>
<option value="BlackBerry">BlackBerry</option>
<option value="HTC">HTC</option>
<option value="Huawei">Huawei</option>
<option value="LG">LG</option>
<option value="Motorola">Motorola</option>
<option value="Nokia">Nokia</option>
<option value="Samsung">Samsung</option>
<option value="Sony">Sony</option>
</select>


// Set the onchange event for the new dropdown
$("#Phones").change(function() { checkSelected(); });

// Run the onchange event to perform any default actions necessary
checkSelected();

function checkSelected() {
// You could use the value of the dropdown in another SPServices request
// here or just perform some other code



 var m = $('#Phones').find("option:selected").val();
 if (m !== 'Select')
 {

 var url = "/m.aspx?m=" + m;
 window.location = url;
 alert(m);
//Tried selecting the selected value here but doesn't work
$('#Phones').children('option:selected').attr("selected","selected");


}

}

3 Answers 3

1

The problem is that when you assign to windows.location a new instance of the page is loaded, so anything that happened on the old is forgotten

So you need to check for the query string parameter on load of the page and use it to set the select (before you attach your event handler)

By the way you should always use jQuery in noConflict mode when using it on a system which includes other javaScript libraries which use $ (like ASP.NET AJAX which SharePoint loads)

0

Is the select option default of course. Why don't you use jQuery Cookie (see here http://www.electrictoolbox.com/jquery-cookies/) or the Javascript session variables (see here http://www.thomasfrank.se/sessionvars.html) to store the currently selected value and restore it upon reload, instead of head-banging to read the Query String, which we know it all, with SharePoint it never get's easier, but on contraire!

Hope it helps, C:\Marius

0

This is my fix. I used the below to get the Querystring parameter:

<script type="text/javascript">
function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}
</script>

and then

 $("#Phones option").each(function(){

   var b = $(this).text();

   var a = getParameterByName("s");
        //alert(a + " - " + b);
       //Check to see if the querystring param equals the each of the options 
       if(a==b)
        {
        $("#Phones  option[value='" + a + "']").attr("selected", "selected")

        }

Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.