0

hello i have This form in domain.com/inicio

<form id="myForm" action="/%27/ajaxform" method="POST" >
  <select id="tiendaseleccionada" style="width: 350px;">
                 <option value="0"
           >
           Seleccione CDD...
        </option>
                 <option value="1"
           >
           Centro de Distribución Directa &quot;1&quot;
        </option>
                 <option value="2"
           >
           Centro de Distribución Directa &quot;2&quot;
        </option>
                 <option value="3"
           >
           Centro de Distribución Directa &quot;3&quot;
        </option>
                 <option value="4"
           >
           Centro de Distribución Directa &quot;4&quot;
        </option>
                 <option value="5"
           >
           Centro de Distribución Directa &quot;5&quot;
        </option>
                 <option value="6"
           >
           Centro de Distribución Directa &quot;6&quot;
        </option>
           </select>
  <img src="/img/loadin2.ajax.gif" style="display: none;" id="imggif">

  <script type="text/javascript">
    jQuery(document).ready(function() {
  jQuery("#tiendaseleccionada option[value='0']").attr('selected', 'selected');
  });
  /*var lista = document.getElementById('tiendaseleccionada');

lista.onchange = function() {
var url = lista.options[lista.selectedIndex].getAttribute('data-url');
window.location = url;
};*/
</script>
<input type="submit" value="Verificar disponibilidad" hidden="true"/>
</form>

ok, then I need to prepopulate just using the url to when page loads it loads with value 2 example. hope there is any way I can do this. I have been searching for this for a long time :)

thanks you.

MMM
  • 7,221
  • 2
  • 24
  • 42
Oswaldo C.
  • 99
  • 7
  • Cannot be done until you don't own the site, this is not like hashes which you can include in your url and correspond to a dom element having id, such as http://example.org/page.php#section-x – Alcalyn Jul 17 '14 at 15:38

3 Answers3

0

With an URL like this:

mydomain.com/mypage#2

You can then read the hash value:

var sel = document.location.hash;

Then you can set the selectedIndex of your SELECT statement based on that value.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
0

Depending on your JQuery version, the Javascript portion of what you're trying to do should be something like this:

jQuery(document).ready(function() {
  jQuery("#tiendaseleccionada").val(2);
});

To dynamically get the select value (2 in the example above) you can read values from the URL querystring:

jquery get querystring from URL

Community
  • 1
  • 1
oliakaoil
  • 1,615
  • 2
  • 15
  • 36
0

Combining the other two answers for clarity:

jQuery(document).ready(function () {
  var sel = document.location.hash;
  jQuery("#tiendaseleccionada").prop('selectedIndex', sel);
});

The code above should get the hash from the end of the URL and then use that value to change the selected index to that index.

It's a little unclear what the exact answer to your issue is unless you provide the specific format of the URL. If you clarify a little more I'll edit my answer.

TheBrogrammer
  • 336
  • 1
  • 3
  • 13