简体   繁体   English

JQuery获取动态值并填充隐藏的表单字段

[英]JQuery getting dynamic values and populate hidden form fields

I have a series of divs that load each has a dynamic id name based on a database result. 我有一系列div,每个div都有一个基于数据库结果的动态id名称。

Within each div are several hidden input text fields that all have the same name and id. 在每个div中有几个隐藏的输入文本字段,它们都具有相同的名称和ID。

The user clicks a href which is dynamically generated from the database launching colorbox. 用户单击从数据库启动颜色框动态生成的href。 For example () 例如 ()

On the opening div is a button. 在开场div是一个按钮。 When the user clicks this button it submits a form. 当用户单击此按钮时,它会提交表单。

As it is now it sumbits only the values of the first instance of the fields (for example Yellow, digital-world, Digital Revolution). 现在它只对第一个字段实例的值进行求和(例如黄色,数字世界,数字革命)。

It is suppsoed to submit the values of the url that is active. 提供的是提交活动的URL的值。 For example, if the user clicks (">) then it should submit Blue, abstract, Abstract Panel. Instead of Yellow, digital-world, Digital Revolution. 例如,如果用户点击(“>)那么它应该提交蓝色,抽象,抽象面板。而不是黄色,数字世界,数字革命。

In addition, there are scroll buttons on the colorbox so for example the user can click a next button to load the next set of values. 此外,颜色框上还有滚动按钮​​,例如用户可以单击下一个按钮来加载下一组值。 Ie. IE浏览器。 if he is on and clicks the next button he is suddenly on so if he clicks the button on here it should submit the correct values ie. 如果他打开并点击下一个按钮,他突然开启,所以如果他点击这里的按钮,它应该提交正确的值,即。 Blue, abstract, Abstract Panel. 蓝色,抽象,抽象面板。

Does this make sense? 这有意义吗?

Let me show... 让我展示...

<div class="doneit">
<div style="float:left;"><a href="#container" id="02_Digital_Revolution_Yellow" target="Yellow" title="digital-world" class="lightbox-image inline"><img src="/videos/digital-world/Yellow/02_Digital_Revolution_Yellow.jpg" ></a></div>
div class="doit"><a href="http://www.url.com/folder/212.htm" class="playvideo"></a></div>
<div style="height:70px;"></div>
<input type="hidden" id="Product_Vid_1" name="Product_Vid_1" value="Yellow">
<input type="hidden" id="Product_Dir_1" name="Product_Dir_1" value="digital-world">
<input type="hidden" id="Product_Name_1" name="Product_Name_1" value="Digital Revolution">
</div>          

<div class="doneit">
<div style="float:left;"><a href="#container" id="06_Abstract_Panel_Blue" target="Blue" title="abstract" class="lightbox-image inline"><img src="/videos/abstract/Blue/06_Abstract_Panel_Blue.jpg"></a></div>
<div class="doit"><a href="http://www.url.com/folder/349.htm" class="playvideo"></a></div>
<div style="height:70px;"></div>
<input type="hidden" id="Product_Vid_1" name="Product_Vid_1" value="Blue">
<input type="hidden" id="Product_Dir_1" name="Product_Dir_1" value="abstract">
<input type="hidden" id="Product_Name_1" name="Product_Name_1" value="Abstract Panel">  
</div>          

<div class="doneit">
<div style="float:left;"><a href="#container" id="10_Abstract_Discs_Red" target="Red" title="abstract" class="lightbox-image inline"><img src="/videos/abstract/Red/10_Abstract_Discs_Red.jpg"></a></div>
<div class="doit"><a href="http://www.url.com/folder/353.htm" class="playvideo"></a></div>
<div style="height:70px;"></div>
<input type="hidden" id="Product_Vid_1" name="Product_Vid_1" value="Red">
<input type="hidden" id="Product_Dir_1" name="Product_Dir_1" value="abstract">
<input type="hidden" id="Product_Name_1" name="Product_Name_1" value="Abstract Disks">  
</div>          

<div class="doneit">
<div style="float:left;"><a href="#container" id="electric-purple-grid" target="Purple" title="electric-effect" class="lightbox-image inline"><img src="/videos/electric-effect/Purple/electric-purple-grid.jpg"></a></div>
<div class="doit"><a href="http://www.url.com/folder/129.htm" class="playvideo"></a></div>
<div style="height:70px;"></div>
<input type="hidden" id="Product_Vid_1" name="Product_Vid_1" value="Purple">
<input type="hidden" id="Product_Dir_1" name="Product_Dir_1" value="electric-effect">
<input type="hidden" id="Product_Name_1" name="Product_Name_1" value="Electric Grid">       
</div>

<div style="display:none">
<div id="container"><div id="video"></div>
<div id="doesit">

<script type="text/javascript">
function submitMyForm(){
$('#Product_color_16').val($('#Product_Vid_1').val());
$('#Product_Dir_16').val($('#Product_Dir_1').val());
$('#Product_Name_16').val($('#Product_Name_1').val());
$('#myform').submit();
}
</script>

<form name="myform" action="/thisurl.asp" method="post">
<input type="hidden" id="Product_color_16" name="Product_color_16" value="">
<input type="hidden" id="Product_Dir_16" name="Product_Dir_16" value="">
<input type="hidden" id="Product_Name_16" name="Product_Name_16" value="">

<button class="addtobutton addtocart" onclick="submitMyForm();"></button>

</form>

</div></div>
</div>

Thanks for any and all help! 感谢您的帮助!

You have multiple elements sharing the same ID - which is wrong and can cause you lots of problems. 你有多个元素共享相同的ID - 这是错误的,可能会导致很多问题。

One of the problems is exactly the one you're facing now. 其中一个问题正是你现在面临的问题。

As you don't have a unique ID for the elements, the code will consider just the first match (in your case, the "Yellow" ones) 由于您没有元素的唯一ID,代码将只考虑第一个匹配(在您的情况下,“黄色”)

To fix this? 解决这个问题? Let's leave as much as possible with jQuery, to make it simple. 让我们尽可能多地使用jQuery,让它变得简单。 Also, let's fix a bit the HTML markup. 另外,让我们修一下HTML标记。 Please refer to the comments. 请参阅评论。

HTML HTML

<!-- Removed all the input ids, because they were duplicated and useless. If you really need them for something else, make them unique. -->

<div class="doneit">
    <div style="float:left;">
        <a href="#container" id="02_Digital_Revolution_Yellow" target="Yellow" title="digital-world" class="lightbox-image inline">
            <img src="/videos/digital-world/Yellow/02_Digital_Revolution_Yellow.jpg" />
        </a>
    </div>
    <div class="doit">
        <a href="http://www.url.com/folder/212.htm" class="playvideo"></a>
    </div>
    <div style="height:70px;"></div>
    <input type="hidden" name="Product_Vid_1" value="Yellow" />
    <input type="hidden" name="Product_Dir_1" value="digital-world" />
    <input type="hidden" name="Product_Name_1" value="Digital Revolution" />
</div>

<div class="doneit">
    <div style="float:left;">
        <a href="#container" id="06_Abstract_Panel_Blue" target="Blue" title="abstract" class="lightbox-image inline">
            <img src="/videos/abstract/Blue/06_Abstract_Panel_Blue.jpg" />
        </a>
    </div>
    <div class="doit">
        <a href="http://www.url.com/folder/349.htm" class="playvideo"></a>
    </div>
    <div style="height:70px;"></div>
    <input type="hidden" name="Product_Vid_1" value="Blue" />
    <input type="hidden" name="Product_Dir_1" value="abstract" />
    <input type="hidden" name="Product_Name_1" value="Abstract Panel" />
</div>

<div class="doneit">
    <div style="float:left;">
        <a href="#container" id="10_Abstract_Discs_Red" target="Red" title="abstract" class="lightbox-image inline">
            <img src="/videos/abstract/Red/10_Abstract_Discs_Red.jpg" />
        </a>
    </div>
    <div class="doit">
        <a href="http://www.url.com/folder/353.htm" class="playvideo"></a>
    </div>
    <div style="height:70px;"></div>
    <input type="hidden" name="Product_Vid_1" value="Red" />
    <input type="hidden" name="Product_Dir_1" value="abstract" />
    <input type="hidden" name="Product_Name_1" value="Abstract Disks" />
</div>

<div class="doneit">
    <div style="float:left;">
        <a href="#container" id="electric-purple-grid" target="Purple" title="electric-effect" class="lightbox-image inline">
            <img src="/videos/electric-effect/Purple/electric-purple-grid.jpg" />
        </a>
    </div>
    <div class="doit">
        <a href="http://www.url.com/folder/129.htm" class="playvideo"></a>
    </div>
    <div style="height:70px;"></div>
    <input type="hidden" name="Product_Vid_1" value="Purple" />
    <input type="hidden" name="Product_Dir_1" value="electric-effect" />
    <input type="hidden" name="Product_Name_1" value="Electric Grid" />
</div>

<div style="display:none">
    <div id="container">
        <div id="video"></div>
        <div id="doesit">
            <form name="myform" action="/thisurl.asp" method="post">
                <input type="hidden" id="Product_color_16" name="Product_color_16" value="" />
                <input type="hidden" id="Product_Dir_16" name="Product_Dir_16" value="" />
                <input type="hidden" id="Product_Name_16" name="Product_Name_16" value="" />

                <!-- You can just ommit the onclick here. It's gonna work automatically, because it's a submit type. -->

                <button class="addtobutton addtocart" type="submit"></button>
            </form>
        </div>
    </div>
</div>

jQuery (Javascript): jQuery(Javascript):

// Add this bit to your page header, straight into the HTML markup (wrapped
// into script tags) or save into a separate JS file. Up to you.

var setFormValues = function(div) {

    // Getting the inputs values. The ones within the clicked div.
    // We look for the inputs which name starts with Product_...
    // Let's use .prop() instead of .val() because IE sometimes doesn's like it.

    var div = $(div);
    var productVid = $("input[name^='Product_Vid_']", div).prop("value");
    var productDir = $("input[name^='Product_Dir_']", div).prop("value");
    var productName = $("input[name^='Product_Name_']", div).prop("value");

    // Setting the form inputs values.

    $("#Product_color_16").prop("value", productVid);
    $("#Product_Dir_16").prop("value", productDir);
    $("#Product_Name_16").prop("value", productName);     
}

$(function () {

    // When the user clicks on one of the divs.

    $(".doneit").on("click", function () {
        setFormValues($(this));    

        return true;        
    });   

    // When the user clicks the cart button, on the video window.

    $(".addtocart").on("click", function() {

        // Here we need a bit of native Javascript.

        var videoPlaying = document.getElementById("video_video");

        if (typeof videoPlaying != "undefined") {
           var videoSource = videoPlaying.currentSrc;

           if (typeof videoSource != "undefined") {

               var videoSourceFilename = videoSource.split("com/")[1].split(".")[0];

               // Check which div has an image which src 
               // matches the video filename.

               var image = $(document).find("img[src*='" + videoSourceFilename + "']");

               if (typeof image != "undefined") {
                   var divContainer = $(image).parent().parent().parent();

                   if (typeof divContainer != "undefined")
                       setFormValues($(divContainer));
               }
           }
        }

        return true;
    });     
});

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

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