开发者

Jquery onchange Post

Hello i want in the moment i select a value to post to the process.php page . I have this code:

<select name="sweets" id="sweets" >
<option value="1">Chocolate</option>


<option value="2">Taffy</option>

<option value="3">Fudge</option>
<option value="4">Cookie</option>

and jquery:

 $("#sweets").change(function () {
      var str = "";
      $("select option:selected").each(function () {
            str += $(this).val() + " ";
         var q =  $(this).val();
          var dataString = 'q=' + q; 

           });
      $("div").text(str);
        $.ajax({
                    type: "POST",
                    url: "process.php",
                    data: dataString,
                    success: function() {
                                         $('#form').slideUp('slow');
                                        }
                })

    })

    .change();

and i can't make it post开发者_JAVA技巧 the value to process.php.

Any help is appreciated,

thank you


You're declaring dataString in an odd spot, but you can get the .val() of the <select> directly much easier, like this:

$("#sweets").change(function () {
  $("div").text($(this).val());
  $.post("process.php", { q: $(this).val() }, function() {
    $('#form').slideUp('slow');
  });
}).change();

In the above I also converted your code to use $.post() just as a shortcut, but it has the same effect.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜