简体   繁体   English

在HTML中传递选定的值 <select> javascript变量?

[英]Pass the selected value in HTML <select> to a javascript variable?

I am trying to run the js script using the parameters specified on the webpage, when javascript is triggered, a php file will run. 我正在尝试使用网页上指定的参数运行js脚本,当触发javascript时,将运行php文件。 The expected output would be the result from getstats.php. 预期的输出将是getstats.php的结果。 It didn't work for some reason. 由于某种原因,它不起作用。 Is it because app1 is not a global variable? 是否因为app1不是全局变量?

The HTML: HTML:

  <select name="app" id="app1">
  <option value=1>A</option>
  <option value=2>B</option>
  <option value=3>C</option>
  <option value=4>D</option>
  </select>     
  P1: <input type="text" id="p1">
  Start Date: <input type="date" id="dt1">
  End Date: <input type="date" id="dt2">
<input type="submit" id = "submit" value="Run"><br>
<script src="js/global.js"></script>

The javascript code in the global.js: global.js中的javascript代码:

$('input#submit').on('click', function() {
var app1 = document.getElementById("app1").value;
var p1 = $('input#p1').val();
var dt1 = $('input#dt1').val();
var dt2 = $('input#dt2').val();
if ($.trim(p1) != '' && $.trim(app1) != '' && $.trim(dt1) != '' && $.trim(dt2) != '') {
    $.post('getstats.php', {app1: app1, p1: p1, dt1: dt1, dt2: dt2}, function(data1) {
        $('div#stats').html(data1);
    });
  }
});

Thanks for your help! 谢谢你的帮助!

Have a look at this question, this question, this question, and this article... 看看这个问题, 这个问题, 这个问题和这篇文章...

You might also like to check out the .serialize() jquery function, which can help save you a lot of time, especially with validation. 您可能还想查看.serialize()jQuery函数,该函数可以帮助您节省很多时间,尤其是在进行验证时。 http://api.jquery.com/serialize/ http://api.jquery.com/serialize/

hope this helps 希望这可以帮助

I think you forgot to use e.preventDefault in the click event. 我认为您忘记了在click事件中使用e.preventDefault。 If the form elements are inside a form, the button wil trigger the value passed to the form action attribute. 如果表单元素在表单内部,则该按钮将触发传递给表单操作属性的值。 This way the value will never be 'passed' to jQuery (JavaScript). 这样,就永远不会将值“传递”给jQuery(JavaScript)。

Also The click handler should be wrapped in a document ready function so it waits untill the DOM is ready before trying to bind the event. 另外,应将Click处理程序包装在文档就绪函数中,以便在尝试绑定事件之前等待DOM准备就绪。

$(document).ready(function() {

    $('input#submit').on('click', function(e) {
        e.preventDefault();

        var app1 = $('#app1').val();
        var p1 = $('input#p1').val();
        var dt1 = $('input#dt1').val();
        var dt2 = $('input#dt2').val();

        if ($.trim(p1) != '' && $.trim(app1) != '' && $.trim(dt1) != '' && $.trim(dt2) != '') {
            $.post('getstats.php', {app1: app1, p1: p1, dt1: dt1, dt2: dt2}, function(data1) {
                $('div#stats').html(data1);
            });
        }
    });
});

You can check values in firebug console with console.log('text'); 您可以使用console.log('text');在firebug控制台中检查值。 or console.log(var); 或console.log(var); Try to log app1 before you start the if statement to see if there is a value in the variable. 在启动if语句之前,尝试记录app1,以查看变量中是否有值。 If it prints undefined, maybe something else is wrong. 如果打印未定义,则可能是其他问题。

I checked your code it's working fine : 我检查了您的代码,一切正常:

Just write your code in 只需编写您的代码

 $(function()
 {
  // Your code
  // This function is exceuted when page is loaded. 
 });

What I tried : 我试过了

$(function()
{
    $('input#submit').on('click', function() {

    var app1 = document.getElementById("app1").value;

    alert("--"+app1);// <------------ showing correct o/p

    var p1 = $('input#p1').val();
    alert(p1);
    var dt1 = $('input#dt1').val();
    var dt2 = $('input#dt2').val();
    if ($.trim(p1) != '' && $.trim(app1) != '' && $.trim(dt1) != '' && $.trim(dt2) != '') {
        $.post('getstats.php', {app1: app1, p1: p1, dt1: dt1, dt2: dt2}, function(data1) {
            $('div#stats').html(data1);
        });
      }
    });


});

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

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