简体   繁体   English

HTML Select标签和jQuery显示和隐藏

[英]HTML Select Tag and jQuery Show and Hide

UPDATE: Looking at people's suggestions, I've realized I should go without AJAX. 更新:看着人们的建议,我意识到我应该没有AJAX。 I decided to change the load function and now put the jquery code inside it. 我决定更改load函数,现在将jquery代码放入其中。 However, it still does not work as no elements are being displayed as visible. 但是,由于没有元素显示为可见,因此它仍然不起作用。 Here is what the new code looks like. 这是新代码的样子。

<style>

.input_1 {display: none;}

.input_2 {display: none;}

.input_3 {display: none;}

</style>

<script type="text/javascript">

var numbers = $('#select_number').val();

function load(numbers) {    
if (numbers == 1) 
{$("input").hide();$(".input_1").show();} else 
if (numbers == 2) 
{$("input").hide();$(".input_1").show();$(".input_2").show();} else
if (numbers == 3) 
{$("input").hide();$(".input_1").show();$(".input_2").show();$(".input_3").show();}}

</script>

And here is what the select tag looks like now 这是select标签现在的样子

<form id="form" action="" method="post">

<select id="select_number" onclick="load( numbers );" >

<option>1</option>
<option>2</option>
<option>3</option>

</select>

<input type="text" class="input_1"/>

<input type="text" class="input_2"/>

<input type="text" class="input_3"/>

</form>
<select id="select_number" onclick="load('ajax_file.php', number_of_places );">

What value is "number_of_places" when you click the element? 单击元素时,“ number_of_places”是什么值? According to your code, it's set once and never changed. 根据您的代码,它只会设置一次,并且永远不会更改。

One solution is to get the value just before sending it: 一种解决方案是在发送值之前获取值:

function load(thefile) {    
    var number = $('#select_number').val();
    $.get(thefile, {number : number});
}

Or get the value on the function call 或者在函数调用中获取值

<select id="select_number" onclick="load('ajax_file.php', $('#select_number').val());">

In addition, you're not performing any action on the result. 此外,您不会对结果执行任何操作。 Maybe you want it that way? 也许您想要那样吗? But if not, you need to add something like this: 但是,如果没有,则需要添加以下内容:

$.get(thefile, {number : number}, function (data){
        alert('The results are: ' + data);
        // do something with the results
    }
);

I hope that helps! 希望对您有所帮助!

I would use jQuery to accomplish such a task. 我将使用jQuery完成此类任务。 jQuery makes it exteremly easy to use AJAX calls. jQuery使使用AJAX调用异常简单。

var myVar = $.get( "ajax_file.php",
                   "/*data to be sent to php file (optional)*/",
                   function(data) { /*whatever you want you do (or simply leave it empty if your php does what you need)*/ },
                  "text" /*data Type*/
                 );

Jquery cannot execute remotely on the server, so the jquery in your Ajax call will do nothing. jQuery无法在服务器上远程执行,因此您的Ajax调用中的jQuery不会执行任何操作。 This is fundamental to the nature of jquery, its not a server side language. 这是jquery本质的基础,它不是服务器端语言。

 function load(thefile) {    
    var number_of_places = $('#select_number').val();
    $.get(thefile, {number : number_of_places}, function(numbers){
        if (numbers = 1) 
           {$("#input_1").show();} else 
        if (numbers = 2) 
           {$("#input_1").show();$("#input_2").show();} else
        if (numbers = 3) 
           {$("#input_1").show();$("#input_2").show();$("#input_3").show();}
    });
  )};

php file: php文件:

  <?php

  if (isset($_GET['number']) === true && empty($_GET['number']) === false) {
       print $_GET['number'];
  }

  ?>

Found out the solution. 找到解决方案。 Add $('#select_number').val() within the load function in the onclick attribute and it works. 在onclick属性的load函数内添加$('#select_number')。val()即可。 Thanks everyone for their help. 感谢大家的帮助。

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

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