gpt4 book ai didi

javascript - jQuery:.append 或 .appendTo PHP 到 HTML 标记内的元素?

转载 作者:行者123 更新时间:2023-12-03 11:12:32 25 4
gpt4 key购买 nike

我正在尝试学习减少我编写的代码量的技术。我有一个相当长的表单,我使用 PHP 填充它。我想知道是否可以在 HTML 标签中附加 PHP。

例如,不要这样写:

<div id="title">Referrals</div>
<input type="hidden" name="REFERRALS" value="n"/>
<input id="wref" type="radio" name="REFERRALS" value='w' <?php echo ($REFERRALS == 'w') ? "checked='checked'" : "" ; ?> />
<label for="wref"></label>
<input id="pref" type="radio" name="REFERRALS" value='p' <?php echo ($REFERRALS == 'p') ? "checked='checked'" : "" ; ?> />
<label for="pref"></label>
<input id="cref" type="radio" name="REFERRALS" value='c' <?php echo ($REFERRALS == 'c') ? "checked='checked'" : "" ; ?> />
<label for="cref"></label>
<hr noshade></hr>

<div id="title">Laundry</div>
<input type="hidden" name="LAUNDRY" value="n"/>
<input id="wlaundry" type="radio" name="LAUNDRY" value='w' <?php echo ($LAUNDRY == 'w') ? "checked='checked'" : "" ; ?> />
<label for="wlaundry"></label>
<input id="plaundry" type="radio" name="LAUNDRY" value='p' <?php echo ($LAUNDRY == 'p') ? "checked='checked'" : "" ; ?> />
<label for="plaundry"></label>
<input id="claundry" type="radio" name="LAUNDRY" value='c' <?php echo ($LAUNDRY == 'c') ? "checked='checked'" : "" ; ?> />
<label for="claundry"></label>
<hr noshade></hr>

<div id="title">Shower</div>
<input type="hidden" name="SHOWER" value="n"/>
<input id="wshower" type="radio" name="SHOWER" value='w' <?php echo ($SHOWER == 'w') ? "checked='checked'" : "" ; ?> />
<label for="wshower"></label>
<input id="pshower" type="radio" name="SHOWER" value='p' <?php echo ($SHOWER == 'p') ? "checked='checked'" : "" ; ?> />
<label for="pshower"></label>
<input id="cshower" type="radio" name="SHOWER" value='c' <?php echo ($SHOWER == 'c') ? "checked='checked'" : "" ; ?> />
<label for="cshower"></label>
<hr noshade></hr>

我想使用 jQuery 语句,例如...

variableName = $("input[name*='value']").val();
$('input[value^=']').append(<?php echo (variableName == 'c') ? "checked='checked'" : "" ; ?>);

将 PHP 附加到每个相关的 HTML 标记中。

但是,我不知道这是否可行,并且尚未在 jQuery api 中找到任何解决方案。

最佳答案

您不能使用 JavaScript(jQuery 是一个 JavaScript 库)以这种方式与 PHP 交互。

PHP 在服务器上进行解析,并将生成的 HTML 发送到您的浏览器进行渲染。

可以使用 JavaScript(和/或 jQuery)与浏览器接收到的 HTML 进行交互,但随后就失去了保存数据的能力,例如在表单提交之后,而不涉及服务器或某些东西。一种客户端存储。

关于javascript - jQuery:.append 或 .appendTo PHP 到 HTML 标记内的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27507167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com