简体   繁体   English

单选按钮开关检查 php

[英]Radio Buttons Switch Checked php

My site is like a search engine with 2 types of search.我的网站就像一个具有 2 种搜索类型的搜索引擎。 Type 1 Type 2. I just dont know how to do it when someone come to my site the default checked type to be Type 1 and if it choose Type2 then remain Type2 for the visitor even if the page is refreshed.类型 1 类型 2。当有人来到我的网站时,我只是不知道该怎么做,默认选中的类型是类型 1,如果它选择类型 2,那么即使页面被刷新,访问者仍然保持类型 2。 What i need exactly is like sample from here.我需要的正是来自这里的样本。

js js

$(function() {
        $( "#radio" ).buttonset();
    });​

html html

<form>
    <div id="radio">
        <input type="radio" id="radio1" name="radio" /><label for="radio1">Choice 1</label>
        <input type="radio" id="radio2" name="radio" checked="checked" /><label for="radio2">Choice 2</label>
        <input type="radio" id="radio3" name="radio" /><label for="radio3">Choice 3</label>
    </div>
</form>

I want to be able to switch the checked="checked" from a radio to another or something like this i think.我希望能够将checked="checked"从收音机切换到另一个或类似的东西。 Really don't know.真的不知道。 But i think it has to do something with php or javascript.但我认为它必须用 php 或 javascript 做一些事情。

You would need to cache/store the set option somewhere (cookie, database, memcache(d), redis, etc.) and load using the user's ID.您需要在某处缓存/存储设置选项(cookie、数据库、memcache(d)、redis 等)并使用用户 ID 加载。 This will also have to be handled in Javascript as it involves the client-side.这也必须在 Javascript 中处理,因为它涉及客户端。

Cookie Reference : https://developer.mozilla.org/en-US/docs/DOM/document.cookie Cookie 参考https ://developer.mozilla.org/en-US/docs/DOM/document.cookie

For memcache(d), redis, SQL, file storage:对于memcache(d)、redis、SQL、文件存储:

AJAX Reference : https://developer.mozilla.org/en-US/docs/AJAX AJAX 参考https ://developer.mozilla.org/en-US/docs/AJAX

you can define function in the onClick event of the radio button:您可以在单选按钮的 onClick 事件中定义函数:

$(document).ready(function() {
  $('radio[name=radio]').each(function() {
   $(this).click(function() {
    my_function();
    });
   });
});

and my_function will send an ajax request to open session to keep the value of checked radio:并且 my_function 将发送一个 ajax 请求来打开会话以保持选中单选的值:

my_function()
{
   var value_checked = $("input[name='radio']:checked").val();
   $.ajax({
   type: 'POST',
   url: 'page.php',
   data: {'value_checked':value_checked},
   });
}

you get in your page.php $_POST['value_checked '] and you set it in a session variable您进入 page.php $_POST['value_checked '] 并将其设置在会话变量中

You have to store that information in a cookie or a session as web is stateless and it will not remember the status of the page if posted back unless it is cached somewhere..您必须将该信息存储在 cookie 或会话中,因为web 是无状态的,并且如果回发它不会记住页面的状态,除非它被缓存在某个地方。

Try using the jQuery cookie plugin尝试使用jQuery cookie 插件

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

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