简体   繁体   English

jQuery获取输入值

[英]jquery get value of input

My php code 我的PHP代码

  <input style="text-align: center;" id="<?php echo $this->get_field_id("nums"); ?>"
            name="<?php echo $this->get_field_name("nums"); ?>" 
            type="text" value="<?php echo absint($instance["nums"]); ?>" size='3' 
    />

and jquery : jquery

 $(document).ready(function(){
        $('.div1').slick({
            slidesToShow: 3,
            slidesToScroll: 1,
        });
    });

How to get value: slidesToShow = $instance["nums"] 如何获得价值: slidesToShow = $instance["nums"]

Thanks 谢谢

Declare your variable as GLOBAL , so that it can be accessed across files(in case php and js are on different pages) 将变量声明为GLOBAL ,以便可以跨文件访问它(以防php和js位于不同页面上)

$GLOBALS['a'] = absint($instance["nums"]);

$(document).ready(function(){
        $('.div1').slick({
            slidesToShow: <?php echo $GLOBALS['a'];?>
            slidesToScroll: 1,
        });
    });

If you're generating the jQuery code from PHP you can just generate the number there: 如果要从PHP生成jQuery代码,则可以在此处生成数字:

slidesToShow: <?php echo absint($instance["nums"]); ?>,

but you're probably not, if you run the jQuery code after the HTML is loaded you could try: 但是可能不是,如果在HTML加载后运行jQuery代码,则可以尝试:

slidesToShow: $("#field_id").val(),

Where *field_id* is the generated id for the field. 其中* field_id *是字段的生成ID。 If you don't know that (because it's dynamic from the PHP) you either need to set a variable with the field ID, or if you're doing that you can set a global in the PHP: 如果您不知道(因为它在PHP中是动态的),则需要使用字段ID设置变量,或者如果您要这样做,则可以在PHP中设置全局变量:

<script language='javascript'>
    var global_instance_nums = <?php echo absint($instance["nums"]); ?>;
</script>

and then use later in the javascript: 然后在javascript中稍后使用:

slidesToShow: global_instance_nums,

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

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