简体   繁体   English

单击按钮时使用Jquery创建隐藏元素

[英]Using Jquery to create a hidden element when button is clicked

I have a form that is submitted to the server using jscript however i need to post the value of the submit button. 我有一个使用jscript提交给服务器的表单但是我需要发布提交按钮的值。 This cannot be done the conventional way i must use jscript to post form. 这不能用传统方式完成我必须使用jscript发布表单。 What i am doing is when the button is clicked i want to create a hidden text item with the value of the button and post that to the server. 我正在做的是当单击按钮时我想创建一个带有按钮值的隐藏文本项并将其发布到服务器。 I am having some problems in creating and attaching the element to the form: 我在创建元素并将其附加到表单时遇到一些问题:

JScript JScript中

 $(document).ready(function(){      

    $('#query').click(function(e){

         var form = $('citizenRegistration');


          var self= $("#query"),newElement = $("<input type='hidden'/>");

          alert("self value is : "+self.val());

                 //create a new element and copy attributes             
                newElement
                    .attr("name", 'user_request2')
                    .val(self.val())
                    .appendTo('.buttons');

            alert(newElement.attr("name"));

        e.preventDefault();
        //alert($(this).val());
        //submitPage();

    });

});

HTML HTML

 <div class="buttons">  
    <ol>
    <li><input id="save" type="submit" name= "user_request" value="Save"/>
    <input id="update" type="submit" name= "user_request" value="Update"/>
    <input id="query" type="submit" name= "user_request" value="Query"/>
    </li>       
    </ol>
    </div>

Specify the attribute type in the call to attr() not in the construction of the element. 在对attr()的调用中指定属性type ,而不是在元素的构造中。 Also the selector for citizenRegistration is missing either a . 此外, citizenRegistration的选择器也缺少a . or a # depending on whether it is an id or class. 或者#取决于它是id还是class。

   $(document).ready(function(){
        $("#query").click(function(e){
             var form = $("#citizenRegistration"); //Added # may need .

              //removed type=hidden, you can just use $(this)
              var self= $(this),newElement = $("<input/>");
                     //create a new element and copy attributes             
                    newElement.attr({
                            "name":"user_request2",
                            "type":"hidden" //added new attribute
                     }).val(self.val()).appendTo(".buttons");

            e.preventDefault();
        });
    });

Working Example: http://jsfiddle.net/BXqVP/1/ 工作示例: http //jsfiddle.net/BXqVP/1/

You can see the hidden element when inspecting the page with firebug: 使用firebug检查页面时,您可以看到隐藏的元素:

在此输入图像描述

Without seeing your HTML I can't be certain, but I see two possible problems: 没有看到你的HTML我不能确定,但​​我看到两个可能的问题:

var form = $('citizenRegistration');

This is not a valid selector. 这不是有效的选择器。 It should probably start with # or . 它应该以#或开头. . You don't seem to end up using it, though. 但是,你似乎最终没有使用它。

.appendTo('.buttons');

If .buttons is an input element, you can't append to it. 如果.buttons是输入元素,则无法附加到它。 You also shouldn't be appending to multiple .buttons elements, if there is more than one. 如果有多个.buttons元素,也不应该附加多个.buttons元素。 Instead, you should probably append it to form . 相反,您应该将其附加到form Just .appendTo(form) will work fine. 只需.appendTo(form)就可以了。

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

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