我如何在 onclick 函数中传递 3 个参数。参数在 php 标签内

How can i pass 3 parameters in onclick function .The parameters are inside php tag

这是我的 onclick 函数:

onclick="InboxDetailsPage('<?php echo $message['id']; ?>')

这些是我的参数:threadidrecipient idmessage id

<input type="hidden" id="threadId-<?php echo $message['id']; ?>" value="<?php echo $message['threadId']; ?>"/>
<input type="hidden" id="recipientId-<?php echo $message['id']; ?>" value="<?php echo $message['otherUserRoleId']; ?>"/>
<?php echo $message['id']; ?>

这是我使用我的 onclick 函数的地方:

<div class="inbox">
    <?php $inboxno = 0;
    foreach ($messageList as $message) { ?>
        <?php if ($message['toFromLabel'] == "From") {
            $inboxno++; ?>
            <input type="hidden" id="threadId-<?php echo $message['id']; ?>" value="<?php echo $message['threadId']; ?>"/>
            <input type="hidden" id="recipientId-<?php echo $message['id']; ?>" value="<?php echo $message['otherUserRoleId']; ?>"/>
            <tr class="gradeA odd inbox">
                <td class="" style="padding-bottom: 1px;" <?php if ($message['status'] == MessageStatusEnum::UNREAD){ ?>style="color:#E55B43;" <?php } else { ?>style="color:#000;" <?php }; ?>><?php echo $message['date']; ?></td>
                <td class="" style="padding-bottom: 1px;" <?php if ($message['status'] == MessageStatusEnum::UNREAD){ ?>style="color:#E55B43;" <?php } else { ?>style="color:#000;" <?php }; ?>><?php echo $message['fromName']; ?></td>
                <td class=" " style="padding-bottom: 1px;" <?php if ($message['status'] == MessageStatusEnum::UNREAD){ ?>style="color:#E55B43;" <?php } else { ?>style="color:#000;" <?php }; ?> style="width:300px !important"><?php echo $message['subject'] ?></td>
                <td class="" style="padding-bottom: 1px;"><a type="button" data-toggle="modal" data-target="#myModalq1" class="btn btn-success " onclick="InboxDetailsPage('<?php echo $message['id']; ?>') ">VIEW</a></td>
            </tr>
        <?php }
    } ?>
</div>

您可以执行以下格式:

onclick="InboxDetailsPage('<?php echo $message['id'];?>', '<?php echo $message['id'];?>', '<?php echo $message['id'];?>')

或者,只使用一个回显:

onclick="InboxDetailsPage(<?php echo "'{$message['id']}', '{$message['date']}', '{$message['id']}'";?>)

您可以尝试以下方法:

onclick="InboxDetailsPage("<?php echo $message['id'];?>","threadId-<?php echo $message['id']; ?>","recipientId-<?php echo $message['id']; ?>")

你可以使用这个方法:

    <a href="#" onclick="myFunction('parameter1', 'parameter', 'parameter3');">click me</a>
<script>
    function myFunction(parameter1, parameter2, parameter3) {
      alert(parameter1);
    }
</script>
onclick="InboxDetailsPage(
    '<?php echo $message['id'];?>', 
    '<?php echo $message['id'];?>', 
    '<?php echo $message['id'];?>'
)

另一种选择可能是 JSON 对选项数组进行编码,并发送参数以避免长参数列表并提高可读性。

$params=[
   'thread_id'=>$message['thread_id'],
   'recipient_id'=>$message['recipient_id'],
   'id'=>$message['id']
];

<input type="text" onclick="InboxDetailsPage('<?=json_encode($params)?>')">

然后使用它

function InboxDetailsPage(options){
     console.log(options.thread_id,options.recipient_id,options.id);
}