gpt4 book ai didi

javascript - 在 jquery、javascript 和 html 中获取隐藏值并进行匹配

转载 作者:行者123 更新时间:2023-12-03 07:53:45 24 4
gpt4 key购买 nike

我有一个模板,其中包含 2 个隐藏字段和 1 个复选框。使用函数 addProductImage() 呈现模板并将其附加到 html 页面。

如何获取隐藏字段(thisFile 和 mainImage)的值? div 的 id 是动态的 gc_photo_{{id}}_{{filename}} ,它看起来像这样 gc_photo_1234_12dhbc.jpg

获取两个隐藏字段的值后,我想匹配这两个值,看看是否 ===,如果是,则将复选框设置为选中。

模板代码

    <script type="text/template" id="imageTemplate">
<div class="row gc_photo" id="gc_photo_{{id}}_{{filename}}" style=" border-bottom:1px solid #ddd; padding-bottom:20px; margin-bottom:20px;">
<div class="col-md-2">
<input type="hidden" name="thisFile" value="{{filename}}"/>
<input type="hidden" name="mainImage" value="<?php echo $biz_product->image_name;?>"/>
<input onclick="return set_parentimage(this);" type="radio" name="primary_image" id="{{id}}_{{filename}}" value="{{id}}" {{#primary}}checked="checked"{{/primary}} /> <?php echo lang('main_image');?>
</div>
</div>
</script>

函数代码

function addProductImage(val)
{
view = {
id:val.item_id.replace(/\s/g, ''),
filename:val.detail_image_name,
alt:val.alt,
primary:val.primary,
caption:val.caption
}

var output = Mustache.render(imageTemplate, view);

$('#gc_photos').append(output);
$('#gc_photos').sortable('refresh');

thisFile = $($('#gc_photo_id_filename').find("input")[0]).val();
mainImage = $($('#gc_photo_id_filename').find("input")[1]).val();


alert(thisFile);
alert(mainImage);
if(thisFile === mainImage)
{
alert('true');
}
else
{
alert("different");
}

photos_sortable();
}
}

更新1函数代码

function addProductImage(val)
{
view = {
id:val.item_id.replace(/\s/g, ''),
filename:val.detail_image_name,
alt:val.alt,
primary:val.primary,
caption:val.caption
}

var output = Mustache.render(imageTemplate, view);

$('#gc_photos').append(output);
$('#gc_photos').sortable('refresh');

thisFile = $($('#gc_photo_id_filename').find("input")[0]).val();
mainImage = $($('#gc_photo_id_filename').find("input")[1]).val();


alert(thisFile);
alert(mainImage);
if(thisFile === mainImage)
{
$('[name="primary_image"]').prop('checked', true);
}
else
{
alert("different");
}

photos_sortable();
}
}

最佳答案

thisFile = $(output).find("[name='thisFile']").val();
mainFile = $(output).find("[name='mainFile']").val();

你可以通过这种方式获取thisFile和mainFile的值。

关于javascript - 在 jquery、javascript 和 html 中获取隐藏值并进行匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890951/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com