开发者

how to send a value from a HTML dropdown box to a JQuery function

开发者 https://www.devze.com 2023-01-24 22:13 出处:网络
I\'m very new to JQuery and don\'t have much experience with javascript at all, and after hours of trying to figure this out i give开发者_运维技巧 up. I know how to do it in PHP, but there\'s somethin

I'm very new to JQuery and don't have much experience with javascript at all, and after hours of trying to figure this out i give开发者_运维技巧 up. I know how to do it in PHP, but there's something I'm missing when I try it in JQuery.

I have a html-file that generates a dorpdown box that looks something like tihs:

<form action="" method="GET">
  <select id="countiesList" name="counties">
    <option value="all" selected="selected">All Counties</option>
    <option value="county1">County 1</option>
    <option value="county2">County 2</option>
    ...
  </select>
</from>
  1. How do I get the selected value from the dropdownbox in to a JQuery function? Do I have to refer to the function I want to use in the form?
  2. Should I have a submit button? (I'd prefer not to)

--edited-- My point is that I want to find out which of the options the user selects, so that I can use it in another function.

Thanks a lot!


It sounds like you want to submit it immediately on change. In that case use the change event like this:

$(function() { //run when the DOM is ready
  $("#countriesList").change(function() { //this runs when the selection changes
    $.post("myPage.php", { country: $(this).val() }, function(data) {
      //use data here
    });
  });
});

What this does is when your selection changes, we post to "myPage.php" which gets a POST variable of country, which will be the value you picked (all, country, etc). Just get this from $_POST["country"] and render your response...which will be data in the above $.post() callback. You can then do whatever you want with that response, for example if it's another <select> with state, you could append that to somewhere, for example:

$("#nextDropDownContainer").html(data);
0

精彩评论

暂无评论...
验证码 换一张
取 消