Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选择option,然后根据所选的';s选项_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 选择option,然后根据所选的';s选项

Javascript 选择option,然后根据所选的';s选项,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,这让我快发疯了。我有一个带有不同值的选择选项。它们需要每秒作为POST ajax请求发送到PHP文件,并打印输出。ajax调用工作正常,我无法处理selecton.change函数。有人能帮我吗 index.html <html> <head> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js

这让我快发疯了。我有一个带有不同值的
选择
选项。它们需要每秒作为POST ajax请求发送到PHP文件,并打印输出。ajax调用工作正常,我无法处理
select
on.change函数。有人能帮我吗

index.html

<html>
    <head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
        <script type="text/javascript">
            var currency = '';

            $('#currency').on('change', function () {
                currency = this.value;
                reload_script()
            });

            function repeatAjax() {
                jQuery.ajax({
                    type: 'POST',
                    url: 'ajax.php',
                    data: { 'currency': currency },
                    dataType: 'text',
                    success: function (e) {
                        $('#output').html(e)
                    },
                    complete: function () {
                        setTimeout(repeatAjax, 1000)
                    }
                })
            }
            repeatAjax()
        </script>
    </head>
    <body>
        <b>Please select currency: </b>
        <br />
        <select name="currency" id="currency">
            <option value="">-- Please Select --</option>
            <option value="BTC/USD">BTC/USD</option>
            <option value="BTC/EUR">BTC/EUR</option>
            <option value="BTC/GBP">BTC/GBP</option>
        </select>
        <br />
        <b>Result: </b>
        <div id="output"></div>
    </body>
</html>

var货币=“”;
$('#currency')。关于('change',函数(){
货币=此值;
重新加载脚本()
});
函数repeatAjax(){
jQuery.ajax({
键入:“POST”,
url:'ajax.php',
数据:{“货币”:货币},
数据类型:“文本”,
成功:职能(e){
$('#output').html(e)
},
完成:函数(){
setTimeout(repeatAjax,1000)
}
})
}
重复ajax()
请选择货币:

--请选择-- BTC/美元 BTC/欧元 BTC/英镑
结果:
ajax.php

<?php
if(isset($_POST['currency'])) {
    switch($_POST['currency']) {
        case 'BTC/USD':
            echo 'Current currency of BTC/USD: 0.000 / 0.000';
            break;
        case 'BTC/EUR':
            echo 'Current currency of BTC/EUR: 0.000 / 0.000';
            break;
        case 'BTC/GBP':
            echo 'Current currency of BTC/GBP: 0.000 / 0.000';
            break;
        default:
            echo 'Please select currency first.';
            break;
    }
}
?>

我想这就是你想要的:

var currency = '';

            $('#currency').on('change', function () {
                currency = $( this ).val(); // <-----------this has been changed
                repeatAjax(); // <----- this function has changed 
            });

            function repeatAjax() {
                jQuery.ajax({
                    type: 'POST',
                    url: 'ajax.php',
                    data: { 'currency': currency },
                    dataType: 'text',
                    success: function (e) {
                        $('#output').html(e)
                    },
                    complete: function () {
                        setTimeout(repeatAjax, 1000)
                    }
                })
            }
            repeatAjax()
var货币=”;
$('#currency')。关于('change',函数(){

currency=$(this.val();//
this.value
应该是
$(this.val()
,或者如果需要文本部分
$(this.text()
,也可以是
重新加载脚本()
似乎什么都没做……你把纯javascript和jquery混合了一点。@No1\u Melman--
这个.value
是正确的-没有理由把
这个
包装成一个jQobject@tymeJV如果OP希望以某种方式扩展此功能,那么从长远来看,将其作为jQuery对象可能会更好很显然,他们做了同样的事情。