简体   繁体   English

将javascript onclick消息添加到php动态列表

[英]Add javascript onclick message to php dynamic list

I have a dynamically-generated list of plants that I have gotten from a MySQL query. 我有一个动态生成的植物列表,这些列表是从MySQL查询中获得的。 In each row of the resulting list, I have a select dropdown list of plant sizes to allow users to change the size of each plant. 在结果列表的每一行中,我都有一个植物大小的选择下拉列表,以允许用户更改每个植物的大小。

I would like to warn the user upon clicking on the dropdown list that they need to click the submit button to update when they change the size any given plant. 我想在单击下拉列表时警告用户,当他们更改任何给定植物的大小时,他们需要单击“提交”按钮进行更新。 The current JavaScript I have plugged in displays the correct message only in the first row of results, no matter which row I click the select dropdown in. 我插入的当前JavaScript仅在结果的第一行中显示正确的消息,无论我单击选择下拉列表中的哪一行。

Here is the code that currently works and will only appear the first row of the list. 这是当前有效的代码,将仅出现在列表的第一行。 I realize this is old syntax, but I see so much diverse information out there, that I'm having a hard time sorting out what to do. 我意识到这是古老的语法,但是我看到的信息太多了,以至于我很难确定要做什么。 I am much better at php than JavaScript. 我在PHP方面比JavaScript更好。

$(document).ready(function() { 
$(".plantSize").click(function() { 
    $("#reCalc").css({ color:"red" }).html("Click button to update after making changes.").width(400); 
});});



echo '<td>

            <select class="plantSize" name="size_' . $row_count . '" value="' . $size . '">
                  <option value=".5"' . ($size == .5 ? ' selected="selected"' : '') . '>.5 feet</option>
                  <option value="1"' . ($size == 1 ? ' selected="selected"' : '') . '>1 foot</option>
                  <option value="2"' . ($size == 2 ? ' selected="selected"' : '') . '>2 feet</option>
                  <option value="3"' . ($size == 3 ? ' selected="selected"' : '') . '>3 feet</option>
                  <option value="4"' . ($size == 4 ? ' selected="selected"' : '') . '>4 feet</option>
                  <option value="5"' . ($size == 5 ? ' selected="selected"' : '') . '>5 feet</option>
                  <option value="6"' . ($size == 6 ? ' selected="selected"' : '') . '>6 feet</option>
                  <option value="7"' . ($size == 7 ? ' selected="selected"' : '') . '>7 feet</option>
                  <option value="8"' . ($size == 8 ? ' selected="selected"' : '') . '>8 feet</option>
                  <option value="9"' . ($size == 9 ? ' selected="selected"' : '') . '>9 feet</option>
                  <option value="10"' . ($size == 10 ? ' selected="selected"' : '') . '>10 feet</option>
            </select>
    </td>

    <td>'; 
              $peak_water = $peak_et * $etaf * 0.7854 * 1 * .623;
              echo number_format($peak_water,1) . ' gal./wk.</td>'; 
    </td>

    <td><input type="submit" name="Submit" value="Calculate" />
                <span class="recalcwarning" id="reCalc"></span>
    </td>

    </tr>'; 

The ID field is supposed to be unique for ENTIRE DOCUMENT. ID字段对于整个文档应该是唯一的。 Not just on each row. 不只是每一行。 JQuery is matching all of the #reCalc ID in the whole table, then is only applying the HTML text only to the first matched #reCalc which is the one in the first row. jQuery匹配整个表中的所有#reCalc ID,然后仅将HTML文本仅应用于第一个匹配的#reCalc ,即第一行中的那个。 Here are a couple of ways to fix it. 这里有几种修复方法。

1) If you can't modifiy the ID, look for the reCalc ID in only the row of the click. 1)如果您无法修改ID,请仅在点击的行中查找reCalc ID。

$(".plantSize").click(function() { 
    //finds the closest TR up-DOM of the clicked object, then finds the span with ID reCalc within that TR.
    var span = $(this).closest("tr").find("span#reCalc"); 
    span.css({ color:"red" }).html("Click button to update after making changes.").width(400); 
});

2) But really, you shouldn't use the ID field for this. 2)但实际上,您不应为此使用ID字段。 ID's are really meant to be unique. ID确实是唯一的。 Instead, use the class name then employ the above code again, but a slightly modified selector. 而是使用类名,然后再次使用上述代码,但选择器要稍加修改。

<span class="recalcwarning"></span>

$(".plantSize").click(function() { 
    //finds the closest TR up-DOM  of the clicked object, then finds the span with class recalcwarning within that TR.
    var span = $(this).closest("tr").find("span.recalcwarning"); 
    span.css({ color:"red" }).html("Click button to update after making changes.").width(400); 
});

update 更新

On further review, I did notice a potential invalid HTML issue. 在进一步审查中,我确实注意到了潜在的无效HTML问题。 In the line echo number_format($peak_water,1) . ' gal./wk.</td>' 在行中echo number_format($peak_water,1) . ' gal./wk.</td>' echo number_format($peak_water,1) . ' gal./wk.</td>' I see that the TD tag is ended there, but then on the next line, you have another end TD tag. echo number_format($peak_water,1) . ' gal./wk.</td>'我看到TD标签到此结束,但是在下一行,您还有另一个TD结束标签。

Here is a link to a workin JSFiddle example based on your sample code. 这是一个基于您的示例代码的workin JSFiddle示例的链接。 http://jsfiddle.net/7v90kj9p/ http://jsfiddle.net/7v90kj9p/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM