繁体   English   中英

如果选择了特定的选择选项,则获取输入值

[英]if specific select option is chosen get the value of input

我对php有一点了解,但这就是我想要做的。 我有一个带有select optionform 如果选择一个值为“ example_5”的选项,我希望它获得<input>的值。 这是用php或jquery使用输入值完成的吗?

例如:某人选择example_5。 他们现在可以在<input>编写一些内容。 并将使用该值。

<script type="text/javascript">
    $(function(){
        if($('select').find('option:selected').val() == 'example_5'){

            use input??

            $_COOKIE['input_example'] = $example_1;

        }
        else{
            $_COOKIE['example_1'] = $example_1;
        }
    });
</script>


<?php
    $_COOKIE['example_1'] = $example_1;
?>

<form method="get" action="page2.php">
    <select name="example1">
        <option value="example_1">example_1</option>
        <option value="example_2">example_2</option>
        <option value="example_3">example_3</option>
        <option value="example_4">example_4</option>
        <option value="example_5">example_5</option>
    </select>
    <input name="input_example" type="text">
</form>

我要做的方法是在select框上有一个onChange侦听器,然后使用javascript通过document.getElementById获取输入框的值。 您可以使用任何所需的jQuery快捷方式,但方法相同。

有关使用此技术的功能示例,请参见此小提琴

无关紧要的是,请确保始终关闭您的<input />标记(可能只是拼写错误:)

编辑

根据评论:只需从cookie的值中设置输入的值。

document.getElementById("input_example").value = getCookie("cookie_name");

在这里您可以编写一个简单的函数来获取cookie:

function getCookie(Name) {
  var search = Name + “=”
  if (document.cookie.length > 0) { // if there are any cookies
    offset = document.cookie.indexOf(search)
    if (offset != -1) { // if cookie exists
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(”;”, offset)
      // set index of end of cookie value
      if (end == -1)
        end = document.cookie.length
      return unescape(document.cookie.substring(offset, end))
    }
  }
}

此处的 js cookie函数教程。

也可以看看

JS Cookies

首先,您要检查from是否已提交。 之后$_COOKIE['example_1'] = $example_1; 将是这样的:

$_COOKIE['example_1'] = $_GET['example1'];

根据您对blo0p3r答案的评论。

在page2.php中,在发送任何空格或HTML之前,在脚本的顶部添加以下内容:

<?php

if (isset($_GET['example1']) && $_GET['example1'] === "example_5")
  setcookie($_GET['input_example'], 'example_5', time()+3600, '/');

暂无
暂无

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

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