使用基于$_POST值的if-else语句设置javascript变量


Setting javascript variables with if-else statements based on $_POST value

我有一个包含单选按钮选项的表单,我想用它来指定一组变量的值。

单选按钮如下:

<table style="width:90%">
<tr><td><input type="radio" id="opt1" name ="selectOpt" value="option1"
checked="checked"/> Option 1</td><td><input type="radio" id="opt2" name ="selectOpt"
value="option2"/> Option 2</td></tr>
</table>

我可以用回显单选按钮的值

<p id="selection">
<?php echo $_POST["selectOpt"];?></p>

所以我确信这个变量是结转的。由于我将$_POST值传递给一个元素,以便用户看到所选的值,所以我想我会使用元素ID来获取if-else语句的值。

以下是我的if-else语句:

$(document).ready(function () {
var template = $("#selection").text();
if(template == 'option1'){
var part1 = "beautifulweather";
var part2 = "ishouldgooutside";
}
else if(template == 'option2'){
var part1 = "isthatrain";
var part2 = "imstayinginside";
}
weather = "www.weather.com/" + part1 + "ithink" + part2;

然后,我将此URL传递给一个按钮,该按钮有一个空href,其中包含以下行:

var a = document.getElementById('weatherbtn');
a.href = weather;

然而,每当我点击链接时,part1&第2部分显示为未定义。我在if-else之后使用了document.write(template,part1,part2(,并确认它打印了option1undefinedundefined。

我在这里做错了什么?

已解决

我需要更改的一般格式问题(这里几乎所有人都指出了这一点(:我需要首先声明我的变量,并从if-else语句中分配的变量中删除var关键字(part1/part2(。

我还绕过了

元素,直接在模板变量中传递了echo。工作代码看起来像:

$(document).ready(function (){
var part1, part2, template;
var template = '<?php echo $_POST["selectOpt"];?>';
if(template == 'option1'){
part1 = "beautifulweather";
part2 = "ishouldgooutside";
}
else if(template == 'option2'){
part1 = "isthatrain";
part2 = "imstayinginside";
}

weather="www.weather.com/"+第1部分+"ithink"+第2部分;

谢谢大家,尤其是cdhowie关于JS不在块范围内的注释。这让我发疯了。

以下是我看到的问题:

$(document).ready(function () {
    var template = $("#selection).text();
    //                          ^
    // Missing closing quote mark
    if(template == 'option1'){
        var part1 = "beautifulweather";
        var part2 = "ishouldgooutside";
    }
    else if(template == 'option2'){
        var part1 = "isthatrain";
        var part2 = "imstayinginside";
        // Duplicate definition of variables.  JavaScript is not block-scoped.
    }
    // It's possible that the template variable does not match either string.
    // Therefore, the part1 and part2 variables might not be assigned here.
    weather = "www.weather.com/" + part1 + "ithink" + part2;

修复前两个问题:

$(document).ready(function () {
    // Always declare variables first!
    var template, part1, part2;
    template = $("#selection").text();
    // Add quote mark       ^
    if(template == 'option1'){
        // Remove "var" keyword.
        part1 = "beautifulweather";
        part2 = "ishouldgooutside";
    }
    else if(template == 'option2'){
        // Remove "var" keyword.
        part1 = "isthatrain";
        part2 = "imstayinginside";
    }
    weather = "www.weather.com/" + part1 + "ithink" + part2;

要解决第三个问题(template与任一字符串都不匹配(,请考虑在调试时通过例如alert(JSON.stringify(template))显示template的值。字符串中可能包含一些空白字符(而且很可能是在标记的情况下(。我预计解决方案将是将您的标记更改为:

<p id="selection"><?php echo $_POST["selectOpt"];?></p>
<p id="selection">
<?php echo $_POST["selectOpt"];?></p>

id不是样式的一部分。因此,您必须将其从

标记中删除,否则id将未设置,并且$("#selection)不会返回对象。因此,使用上面的代码,它应该可以工作。

问候

您需要在if/else-之外定义变量

var part1;
var part2;
if(template == 'option1'){
    part1 = "beautifulweather";
    part2 = "ishouldgooutside";
}
else if(template == 'option2'){
    part1 = "isthatrain";
    part2 = "imstayinginside";
}