如何打开新页面并选中特定的单选按钮。

huangapple go评论62阅读模式
英文:

How to open new page with certain radio inputs checked

问题

为了简化,这里有一个示例。我想要从我的首页链接到我的Inputs.html,并选择其中一个输入。

例如,在第一个链接上,我想要打开Inputs页面,并选择“红色”单选框,第二个链接选择绿色,依此类推。

到目前为止,站点上只使用了html、js和css,服务器上有一些php。

Inputs.html

...
<input type="radio" name="color" id="red" value="red" />
<label...>
<input type="radio" name="color" id="green" value="green" />
<label...>
<input type="radio" name="color" id="blue" value="blue" />
<label...>
...
英文:

For simplicity, here is an example. I want to have a link from my index page to my Inputs.html with one of the inputs selected.

For example, on the first link I want to open the Inputs page with "red" radio input to be checked, on the second green and so on.

So far only html, js and css are used on site, with some php on server

Inputs.html

...
<input type="radio" name="color" id="red" value="red" />
<label...>
<input type="radio" name="color" id="green" value="green" />
<label...>
<input type="radio" name="color" id="blue" value="blue" />
<label...>
...

答案1

得分: 1

index.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <a href="input.php?color=red">红色</a><br/>
    <a href="input.php?color=green">绿色</a><br/>
    <a href="input.php?color=blue">蓝色</a><br/>
</body>
</html>

inputs.php

<?php
$color = $_GET['color'] ?? '';
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="radio" name="color" id="red" value="red" <?php if($color=='red') echo("checked") ?>>红色<br/>
    <input type="radio" name="color" id="green" value="green" <?php if($color=='green') echo("checked") ?>>绿色<br/>
    <input type="radio" name="color" id="blue" value="blue" <?php if($color=='blue') echo("checked") ?>>蓝色<br/>
</body>

</html>
英文:

Hope this will help you

index.php

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;a href=&quot;input.php?color=red&quot;&gt;Red&lt;/a&gt;&lt;br/&gt;
    &lt;a href=&quot;input.php?color=green&quot;&gt;Green&lt;/a&gt;&lt;br/&gt;
    &lt;a href=&quot;input.php?color=blue&quot;&gt;Blue&lt;/a&gt;&lt;br/&gt;
&lt;/body&gt;
&lt;/html&gt;

inputs.php

&lt;?php
$color = $_GET[&#39;color&#39;] ??&#39;&#39;;
?&gt;

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;red&quot; value=&quot;red&quot; &lt;?php if($color==&#39;red&#39;) echo(&quot;checked&quot;) ?&gt;/&gt;Red&lt;br/&gt;
    &lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;green&quot; value=&quot;green&quot; &lt;?php if($color==&#39;green&#39;) echo(&quot;checked&quot;) ?&gt;/&gt;Green&lt;br/&gt;
    &lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;blue&quot; value=&quot;blue&quot; &lt;?php if($color==&#39;blue&#39;) echo(&quot;checked&quot;) ?&gt;/&gt;Blue&lt;br/&gt;
&lt;/body&gt;

&lt;/html&gt;

答案2

得分: 0

你可以使用jQuery选择该单选按钮并将其选中:

$("&quot;#red&quot;).prop("&quot;checked&quot;, true);

英文:

You can use jquery to select that radio button and get it checked:

$(&quot;#red&quot;).prop(&quot;checked&quot;, true);

答案3

得分: 0

查询参数 允许URL包含应选择哪个单选按钮。单选按钮可以在后端或前端中选择。

假设查询参数 color=VALUE(其中 VALUE 是单选按钮的值)应预先选择匹配的单选按钮。

在PHP 中,您可以这样选择:

&lt;?php $color = $_GET[&quot;color&quot;] ?? &quot;&quot;; ?&gt;
&lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;red&quot; value=&quot;red&quot;
  &lt;?php if ($color === &quot;red&quot;): ?&gt;checked&lt;?php endif; ?&gt;&gt;
&lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;green&quot; value=&quot;green&quot;
  &lt;?php if ($color === &quot;green&quot;): ?&gt;checked&lt;?php endif; ?&gt;&gt;
&lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;blue&quot; value=&quot;blue&quot;
  &lt;?php if ($color === &quot;blue&quot;): ?&gt;checked&lt;?php endif; ?&gt;&gt;

在JS 中,如果您有表单的引用,可以这样选择:

const searchParams = new URLSearchParams(&quot;?color=green&quot;); // 示例查询字符串
const form = document.forms[0]; // 表单引用

if (searchParams.has(&quot;color&quot;)) {
  const color = searchParams.get(&quot;color&quot;);
  form.elements.namedItem(&quot;color&quot;).forEach(radio =&gt; {
    radio.checked = radio.value === color;
  });
}

或者像这样:

const searchParams = new URLSearchParams(window.location.search);
if (searchParams.has(&quot;color&quot;)) {
  // 因为ID等于值,我们可以这样做:
  const radio = document.getElementById(searchParams.get(&quot;color&quot;));
  radio.checked = true;
}
英文:

Query parameters allow the URL to include what radio button should be selected. The radio button can be selected in the backend or the frontend.

Let's say that the query parameter color=VALUE (where VALUE is a radio button's values) should preselect the matching radio button.

In PHP you could select it like this:

&lt;?php $color = $_GET[&quot;color&quot;] ?? &quot;&quot;; ?&gt;
&lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;red&quot; value=&quot;red&quot;
  &lt;?php if ($color === &quot;red&quot;): ?&gt;checked&lt;?php endif; ?&gt;&gt;
&lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;green&quot; value=&quot;green&quot;
  &lt;?php if ($color === &quot;green&quot;): ?&gt;checked&lt;?php endif; ?&gt;&gt;
&lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;blue&quot; value=&quot;blue&quot;
  &lt;?php if ($color === &quot;blue&quot;): ?&gt;checked&lt;?php endif; ?&gt;&gt;

In JS you could select it like this (should you have a reference to the form):

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

const searchParams = new URLSearchParams(&quot;?color=green&quot;); // Example query string
const form = document.forms[0]; // The form reference

if (searchParams.has(&quot;color&quot;)) {
  const color = searchParams.get(&quot;color&quot;);
  form.elements.namedItem(&quot;color&quot;).forEach(radio =&gt; {
    radio.checked = radio.value === color;
  });
}

<!-- language: lang-html -->

&lt;form&gt;
  &lt;label for=&quot;red&quot;&gt;&lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;red&quot; value=&quot;red&quot;&gt; Red&lt;/label&gt;
  &lt;label for=&quot;green&quot;&gt;&lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;green&quot; value=&quot;green&quot;&gt; Green&lt;/label&gt;
  &lt;label for=&quot;blue&quot;&gt;&lt;input type=&quot;radio&quot; name=&quot;color&quot; id=&quot;blue&quot; value=&quot;blue&quot;&gt; Blue&lt;/label&gt;
&lt;/form&gt;

<!-- end snippet -->

Or like this:

const searchParams = new URLSearchParams(window.location.search);
if (searchParams.has(&quot;color&quot;)) {
  // Because ID equals value, we can do:
  const radio = document.getElementById(searchParams.get(&quot;color&quot;));
  radio.checked = true;
}

huangapple
  • 本文由 发表于 2023年6月8日 12:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定