将变量从javascript传递到python,而无需重新加载页面

时间:2018-12-11 02:15:00

标签: javascript python jquery ajax

我正在使用cherrypy作为Web服务器和JS循环遍历python对象(将它们作为字符串连接到JS代码)并填充optionS,radioS等。 在用户从中选择的网页上, 我被困在将option_value传递给python(???)

我已经阅读了类似的帖子,并且按照建议我使用了ajax片段,但是它重新加载了整个页面,并且丢失了数据。更新:随着option_value弹出AJAX警报更改,但主要问题是相同的-如何将其从JS(frontend)传递给python(backend)?

import cherrypy
class HelloWorld(object):
    @cherrypy.expose
    def index(self, *args, **kwargs):
        print(args)
        print(kwargs)
        html = '''
<!DOCTYPE html><html><head><meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css">
</head>
<body>
<form>
    <select name='test'>
        <option value="change">Change it</option>
        <option value="verify">Verify it</option>
    </select>
</form>
<br>
<p>change above form and after submit below form verify that above still the same</p>
        <form method="POST" id="form1">
            <select name="cars" id="u_choice">
                <option value="volvo">VOLVO</option>
                <option value="audi">AUDI</option>
            </select>
          <input type="submit">
        </form>
<script>
$('form#form1').submit(function (e) {
    e.preventDefault();
    alert(e.target[0].value));
    return false;
    });
</script>
</body>
</html>'''
        return html

cherrypy.quickstart(HelloWorld())

0 个答案:

没有答案