开发者

jquery javascript problem

Could you please take a look and tell why this isnt working i cant figure it out. the problem is with

$("#item_name").val(item_name);
$("#amount").val(course_price);

This should be adding info to 2 hidden inputs (

<input type="hidden" name="item_name" id="item_name"value="">
 <input type="hidden" name="amount" id="amount" value="">

) but they are coming up as blank

also the value from course_name.php?courseID=1 is Course Name,500

Full Javascript

<script>

      $(document).ready(function() {
url = "date_range.php?courseID="+$('#course_name').val();
  $("#dates").load(url)
  url = "course_name.php?courseID="+$('#c开发者_如何转开发ourse_name').val();
   var course_details;
$.get(url, function(data){
    course_details= data;
});
   split_course_details = course_details.split(',');
   course_name=split_course_details[0];
   course_price=split_course_details[1];
   course_date=$("#date_range").val();
   item_name=course_name+' - '+course_date;
   $("#item_name").val(item_name);
    $("#amount").val(course_price);


      });


$('#course_name').change(function() {
    url = "date_range.php?courseID="+$('#course_name').val();
  $("#dates").load(url)
  url = "course_name.php?courseID="+$('#course_name').val();
  var course_details;
$.get(url, function(data){
    course_details= data;
});
   split_course_details = course_details.split(',');
   course_name=split_course_details[0];
   course_price=split_course_details[1];
   course_date=$("#date_range").val();
   item_name=course_name+' - '+course_date;
   $("#item_name").val(item_name);
    $("#amount").val(course_price);
});

</script>


Place your calculation inside the callback function of $.get(), like this:-

$(document).ready(function() {
    url = "date_range.php?courseID=" + $('#course_name').val();
    $("#dates").load(url)
    url = "course_name.php?courseID=" + $('#course_name').val();
    var course_details;
    $.get(url, function(data) {
        course_details = data;
        split_course_details = course_details.split(',');
        course_name = split_course_details[0];
        course_price = split_course_details[1];
        course_date = $("#date_range").val();
        item_name = course_name + ' - ' + course_date;
        $("#item_name").val(item_name);
        $("#amount").val(course_price);
    });

});

$('#course_name').change(function() {
    url = "date_range.php?courseID=" + $('#course_name').val();
    $("#dates").load(url)
    url = "course_name.php?courseID=" + $('#course_name').val();
    var course_details;
    $.get(url, function(data) {
        course_details = data;
        split_course_details = course_details.split(',');
        course_name = split_course_details[0];
        course_price = split_course_details[1];
        course_date = $("#date_range").val();
        item_name = course_name + ' - ' + course_date;
        $("#item_name").val(item_name);
        $("#amount").val(course_price);
    });
});


Try this:

$(function() {

    var cname = $('#course_name'),
        dates = $('#dates'),
        iname = $('#item_name'),
        amount = $('#amount'),
        drange = $('#date_range');

    cname.change(function() {    
        dates.load( 'date_range.php?courseID=' + this.value );

        $.get('course_name.php?courseID=' + this.value, function(data) {
            data = data.split(',');    
            iname.val( data[0] + ' - ' + drange.val() );
            amount.val( data[1] );
        });
    }).change();

});


it seems like you're using url variable twice for different data... try using additional variable..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜