gpt4 book ai didi

javascript - 来自 MySQL while 循环的 JavaScript 和 HTML 标记中的变量

转载 作者:行者123 更新时间:2023-11-29 10:52:03 27 4
gpt4 key购买 nike

如何在 PHP 文件中的 Java 脚本代码中获取 PHP 变量?这段代码位于 PHP while 循环内,所以我知道我必须以某种方式为每个 JS 属性分配来自 mysql 结果的 $id,并将它们传递到 html 的输入,这样每个 html 标签都是单独的彼此。

这是 PHP 文件代码:

<script> 
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>

while ($row = mysql_fetch_array($query))
{
$id = $row['id'];

<select name='alarm_action' id="alarm_action" required>
<option value="">Default Action</option>
<option value='Play Sound'>Play Sound</option>
<option value='Say This'>Say This</option>
<option value='Run Command'>Run Command</option>
<option value='Run Program'>Run Program</option></select>
<input name="file_path" id="file_path" type="text" style="display: none" required/>




<script>
var alarmInput = $('#alarm_action');
alarmInput.on('change', function() {
var file_path = $('#file_path');
file_path.show();
switch ($(this).val()) {
case 'Play Sound':
case 'Run Program':
file_path.attr('type', 'file');
break;
case 'Say This':
case 'Run Command':
file_path.attr('type', 'text');
break;
case '':
file_path.hide();
break;
}
});
</script>

}

最佳答案

只需使用

    <?php 

$id = $row['id'];

?>
<!-- html -->
<p><?php echo $id; ?></p>

在 JavaScript 中添加

<script>
//var id = <?php echo $id; ?>
var alarmInput = $('<?php echo $id; ?>');
alarmInput.on('change', function() {
var file_path = $('#file_path');
file_path.show();
switch ($(this).val()) {
case 'Play Sound':
case 'Run Program':
file_path.attr('type', 'file');
break;
case 'Say This':
case 'Run Command':
file_path.attr('type', 'text');
break;
case '':
file_path.hide();
break;
}
});
</script>

所以只需使用 <?php echo $id; ?>在您想要的标签内,但不要忘记打开和关闭 php 标签

关于javascript - 来自 MySQL while 循环的 JavaScript 和 HTML 标记中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43554434/

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