HTML隐藏输入值随javascript和单击文本而变化

时间:2013-11-25 21:32:06

标签: javascript jquery html dom

好的,所以这似乎应该非常直接,但我已经坚持了几个小时。 目标:如果我上次点击页面的“交付”或“取件”div部分,我想在我的页面上存储一个变量。

我有Jquery使用以下代码来切换页面上显示的内容(部分中的第一个段)。一切都有效,除了.value和.innerHTML:

$(document).ready(function(){
                  $("#pickup_click_link").click(function(){
                    $('#delivery_stuff').fadeOut('fast', function(){
                        $('#pickup_stuff').fadeIn('fast');
                    });
                    $('#delivery_stuff2').fadeOut('fast', function(){
                        $('#pickup_stuff2').fadeIn('fast');
                    });

                    $("#pickup_button").css("background-color","silver");                       
                    $("#delivery_button").css("background-color","white");  
                    window.onload = function(){
                        document.getElementById('pu_del_nav_var').value='Pickup';
                        document.getElementById('pu_del_nav_varTEST').innerHTML='Pickup';
                    }
                  });
                });

                $(document).ready(function(){
                  $("#delivery_click_link").click(function(){
                    $('#pickup_stuff').fadeOut('fast', function(){
                        $('#delivery_stuff').fadeIn('fast');
                    });
                    $('#pickup_stuff2').fadeOut('fast', function(){
                        $('#delivery_stuff2').fadeIn('fast');
                    });
                    $("#pickup_button").css("background-color","white");                        
                    $("#delivery_button").css("background-color","silver"); 
                    delivery_click();
                  });
                });     

我尝试在Div和Input之后的单独函数中放置给我错误的代码(.value =“Pickup / Delivery”和.innerHTML =“Pickup / Delivery”)onload,但我总是获取错误Uncaught TypeEror:无法设置null的属性'value'

function delivery_click() {  
                                    document.getElementById('#pu_del_nav_var').value='Delivery';    
                                    document.getElementById('#pu_del_nav_varTEST').innerHTML='Delivery';    
                                    alert("Delivery!");
                                }

如何让这个javascript工作,以便更改此输入和“temp here”的信息?

<input type="hidden" id="pu_del_nav_var" name="pu_del_nav_var" value="Pickup">
<div name='pu_del_nav_varTEST' id='pu_del_nav_varTEST'>temp here</div>

1 个答案:

答案 0 :(得分:1)

$('#pu_del_nav_var').val('Delivery');
$('#pu_del_nav_varTEST').html('Delivery');

此外,你为什么要混合jquery&amp; amp;带选择器的javascript。