使用JSON语法将参数作为值发送(send parametres as values using JSON syntax)
我想发送一个带参数的网址,这些参数是一个带有javascript的表单所采用的值,我想用JSON来做,但是当我调试时我看到这个错误:未捕获的ReferenceError:名称未定义..
function recup() { var selectElmt = document.getElementById("name"); var selectcat = document.getElementById("msg"); var name = selectElmt.options[selectElmt.selectedIndex].value; var msg = selectcat.options[selectcat.selectedIndex].value; } function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg + $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }调用值名称和消息时,这是一个语法错误,但我不知道如何修复它或在go函数中
i want to send an url with parametres, those parametres are values taken by a form with javascript and i want to use JSON to do it, but when i debug i see this error : Uncaught ReferenceError: name is not defined..
function recup() { var selectElmt = document.getElementById("name"); var selectcat = document.getElementById("msg"); var name = selectElmt.options[selectElmt.selectedIndex].value; var msg = selectcat.options[selectcat.selectedIndex].value; } function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg + $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }it's a syntax error when calling the value name and msg but i don"t know how to fix it or in the go function
最满意答案
你有两个错误,关闭大括号和加号字符,代码应该是:
var msg = "hello"; // i just simplified the value var name = "test"; function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg; $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }更新 :你需要创建名称和消息全局:
var name, msg; function recup() { var selectElmt = document.getElementById("name"); var selectcat = document.getElementById("msg"); name = selectElmt.options[selectElmt.selectedIndex].value; msg = selectcat.options[selectcat.selectedIndex].value; } function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg; $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }并且recup需要在go之前go
You two errors, closing curly brace and plus character, the code shoud be:
var msg = "hello"; // i just simplified the value var name = "test"; function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg; $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }UPDATE: You need to make name and msg global:
var name, msg; function recup() { var selectElmt = document.getElementById("name"); var selectcat = document.getElementById("msg"); name = selectElmt.options[selectElmt.selectedIndex].value; msg = selectcat.options[selectcat.selectedIndex].value; } function go() { // button send who call the function go var p_url="http://mysite.com/class?name=" + name + "&message=" + msg; $.getJSON(p_url, { }).done(function( data ) { $.each(data, function (key, field) { alert(field); }); }); return false; }and recup need to be executed before go
更多推荐
发布评论