简体   繁体   English

改变价值JS,PHP获得旧价值

[英]Change value JS, PHP getting old value

I've a silly problem. 我有一个愚蠢的问题。

Depending on a the visibility of a div I want to set a hidden value 根据div的可见性,我想设置一个隐藏值

if($('#crewMember').is(':visible')) {
  $('#visibility').attr('value', 'hidden')
} else {
  $('#visibility').attr('value', 'visible')
}

This works. 这有效。 I've checked it via FireBug and I can see that the HTML has changed. 我通过FireBug检查了它,我可以看到HTML已经改变了。

But when I try to get this value after form submission I get the original value, not the changed value. 但是当我在表单提交后尝试获取此值时,我得到原始值,而不是更改的值。

echo $_POST['visibility']
//returns default value, not the adjusted valueHow come?

How come? 怎么会?

EDIT SOme example code 编辑 SOme示例代码

<html>
    <script type="text/javascript">
       $(document).ready(function() {

    $('#div').click(function() {
      $('#visibility').val('hidden');
      $('#value').html('hidden value: ' + $('#visibility').val());
    });

    $('#value').html('hidden value: ' + $('#visibility').val());
  });

  <body>
        <form method="post">
            <div id="div">
                click this area to change value
            </div>

            <div id="value"> <!-- This div will show the actual value of the hidden field -->
            </div>
            <input type="hidden" id="visibility" name="visibility" value="initial value" />
            <input type="submit" name="button" value="button" />
        </form>
    </body>
</html>

When I submit this form the $_POST['visibility'] always contains the string 'initial value'. 当我提交此表单时,$ _POST ['visibility']始终包含字符串'initial value'。 Even when I changed the value with JS to 'hidden'; 即使我用JS将值改为'隐藏';

Try using val() instead (im assuming your visibility element is an input element): 尝试使用val()代替(假设您的visibility元素是输入元素):

if($('#crewMember').is(':visible')) {
  $('#visibility').val('hidden');
} else {
  $('#visibility').val('visible');
}

As ManseUK said, Change $("").attr('value',...) to $("").val(...) (see the jQuery documentation for .val) 正如ManseUK所说,将$("").attr('value',...)改为$("").val(...) (参见.val的jQuery文档

But to answer the question as to why , you need to understand how DOM objects work in JavaScript. 但要回答关于原因的问题,您需要了解DOM对象在JavaScript中的工作原理。 For every element (div, p, a, img, form, input, whatever ...) every single element is an object in the DOM. 对于每个元素(div,p,a,img,form,input, 等等 ......),每个元素都是DOM中的一个对象。

Here's the important part: the element has a value, but also each attribute has a value. 这是重要的部分: 元素有一个值,但每个属性都有一个值。 In jQuery, when you use .attr('value',...) , what you are really doing is creating an attribute with the name of 'value'. 在jQuery中,当你使用.attr('value',...) ,你真正在做的是创建一个名为'value'的属性 But this does NOT change the element value, which stays the same. 但这不会改变元素值,它保持不变。

In firebug, it will read the JavaScript attributes and layer it on top of the element, which explains why it showed up that way in firebug. 在firebug中,它将读取JavaScript属性并将其层叠在元素之上,这解释了为什么它在firebug中以这种方式出现。 However, when it comes time to submit the data to the server, the browser will NOT submit the attribute values - only the element's value (which has not changed). 但是,当需要将数据提交给服务器时,浏览器将不会提交属性值 - 仅提供元素的值(未更改)。 But using .val() will change the element value, which is what will be submitted to the server. 但是使用.val()将更改元素值,即提交给服务器的元素值。

Perhaps this will make more sense in raw JavaScript, instead of jQuery: 也许这会在原始JavaScript中更有意义,而不是jQuery:

the equivalent of .attr() is: element.setAttribute("value","..."); .attr()的等价物是: element.setAttribute("value","...");

the equivalent of .val() is: element.value = "..."; 等价的.val()是: element.value = "...";

Do you see the difference? 你看得到差别吗? In .attr() you never changed the value, only the attribute. .attr()您从未更改过值,只更改了属性。

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

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