最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - use popupWindow = window.open with javascript variables - Stack Overflow

programmeradmin5浏览0评论

I have the below script:

<script type="text/javascript">
        function train(roww){
        $.post ('getpeopleinjobs.php',{ 
        postvarposition: form["position"+roww].value,
        postvarjob: form["job"+roww].value,
        postvarperson: form["person"+roww].value, 
        postrow: roww},
            function(output){ 
                popupWindow = window.open('trainingneeded.php?position=postvarposition&amp;job=postvarjob&amp;person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
                    });
                }
</script>

I have tested the script and it works 100%. my problem is that my variables, postvarposition,postvarjob and postvarperson are being passed as text in the URL string rather than the actual variables.

How do I format the line

popupWindow = window.open('trainingneeded.php?position=postvarposition&amp;job=postvarjob&amp;person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')

so that the variables are passed as variables and not as text.

Thanks in advance.

I have the below script:

<script type="text/javascript">
        function train(roww){
        $.post ('getpeopleinjobs.php',{ 
        postvarposition: form["position"+roww].value,
        postvarjob: form["job"+roww].value,
        postvarperson: form["person"+roww].value, 
        postrow: roww},
            function(output){ 
                popupWindow = window.open('trainingneeded.php?position=postvarposition&amp;job=postvarjob&amp;person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
                    });
                }
</script>

I have tested the script and it works 100%. my problem is that my variables, postvarposition,postvarjob and postvarperson are being passed as text in the URL string rather than the actual variables.

How do I format the line

popupWindow = window.open('trainingneeded.php?position=postvarposition&amp;job=postvarjob&amp;person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')

so that the variables are passed as variables and not as text.

Thanks in advance.

Share Improve this question asked Jul 12, 2012 at 16:10 SmudgerSmudger 10.8k29 gold badges107 silver badges180 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You need to concatenate the variables into the string:

'trainingneeded.php?position=' + postvarposition + '&amp;job=' + postvarjob + '&amp;person=' + postvarperson

Edit:

Using your code, you can see that the .post call is made up of 3 parts: a url (getpeopleinjobs.php), parameters to pass (enclosed in {}), and your success handler (the function). Each of these is separate in scope so none of them has access to any of the others. Basically, your success handler has no idea what a "postvarposition" is. In order for it to be visible, you would have to bring the variable outside of its container scope. In this case, you have to remove the assignment of these items from the {} and place them outside the method .post call.

<script type="text/javascript">
    function train(roww){
    $.post ('getpeopleinjobs.php',{ 
    // Because this is inside a {} these variables
    // can be considered "trapped" inside this scope
    postvarposition: form["position"+roww].value,
    postvarjob: form["job"+roww].value,
    postvarperson: form["person"+roww].value, 
    postrow: roww},
        function(output){ 
            popupWindow = window.open('trainingneeded.php?position=postvarposition&amp;job=postvarjob&amp;person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
                });
            }
</script>

A possible solution to this would be:

<script type="text/javascript">
    function train(roww){

        // declare them and assign them out here
        var position = form["position"+roww].value;
        var job = form["job"+roww].value;
        var person = form["person"+roww].value;

        $.post ('getpeopleinjobs.php',{ 
            postvarposition: position,
            postvarjob: job,
            postvarperson: person, 
            postrow: roww },
        function(output){ 
            popupWindow = window.open('trainingneeded.php?position=' + position + '&amp;job=' + job + '&amp;person=' + person,'popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes');
                });
            }
</script>

I was able to get this working, it was indeed a string concatenation that was required.

//some data taken from the google maps api.
resstring=results[0].geometry.location;

var new_window = 'http://www.trystingtrees./coordsc.php?coords='+resstring+'&listing_id=".$listing_id."';

popupWindow=window.open(new_window,'_SELF');
发布评论

评论列表(0)

  1. 暂无评论