Dee*_*end 5 html javascript forms
我有一个JavaScript函数,该函数可使用显示给用户的图像文件名来更新隐藏在字段中的内容。在具有单个图像和单个隐藏字段的页面上,此方法效果很好。我正在尝试对其进行自定义,以便可以在单个页面上使用它来更新多个隐藏字段,具体取决于它们是否存在。
以前我曾尝试从我的手机中调用单独的函数onload用分号分隔。它适用于第一页/隐藏字段,但是当第二页上的第一个隐藏字段不可用时,它一直显示错误。
在这种尝试中,我试图将单个函数与if语句链接到隐藏字段的id一起使用,但是不幸的是,我似乎无法使它在任何页面/隐藏字段上都能正常工作
谁能告诉我我要去哪里错了?我相信可以这样做,但是我没有任何结果。谢谢
电流输出
<input id="id_9-slider_one_image" name="9-slider_one_image" type="hidden" />
<input id="id_10-slider_two_image" name="10-slider_two_image" type="hidden" />
<input id="id_11-slider_three_image" name="11-slider_three_image" type="hidden" />
Run Code Online (Sandbox Code Playgroud)
期望的输出
<input id="id_9-slider_one_image" name="9-slider_one_image" type="hidden" value="P1DP.jpg"/>
<input id="id_10-slider_two_image" name="10-slider_two_image" type="hidden" value="P6D6.jpg"/>
<input id="id_11-slider_three_image" name="11-slider_three_image" type="hidden" value="P3D3.jpg"/>
Run Code Online (Sandbox Code Playgroud)
我的密码
<div class="image_rating">
<img src="{% static "survey/images/pathone/" %}{{display_image}}" value="{{display_image}}" onload="updateInput(this)"/>
</div>
<script type="text/javascript">
function updateInput(ish) {
var valueAttribute = ish.getAttribute("value");
if($(this).attr("id") == "id_9-slider_one_image")
document.getElementById("id_9-slider_one_image").setAttribute(
"value", valueAttribute);
if($(this).attr("id") == "id_10-slider_two_image")
document.getElementById("id_10-slider_two_image").setAttribute(
"value", valueAttribute);
if($(this).attr("id") == "id_11-slider_three_image")
document.getElementById("id_11-slider_three_image").setAttribute(
"value", valueAttribute);
}
</script>
Run Code Online (Sandbox Code Playgroud)
感谢这个问题和最高投票的答案,我能够在尝试设置值之前检查页面中是否存在该 id
{% if wizard.steps.current in steps %}
<div class="image_rating">
<img src="{% static "survey/images/pathone/" %}{{display_image}}" value="{{display_image}}" onload="updateInput(this)"/>
</div>
<script type="text/javascript">
function updateInput(ish) {
var valueAttribute = ish.getAttribute("value");
if (document.getElementById('id_9-slider_one_image')) {
document.getElementById("id_9-slider_one_image").setAttribute(
"value", valueAttribute)
}
if (document.getElementById('id_10-slider_two_image')) {
document.getElementById("id_10-slider_two_image").setAttribute(
"value", valueAttribute)
}
if (document.getElementById('id_11-slider_three_image')) {
document.getElementById("id_11-slider_three_image").setAttribute(
"value", valueAttribute)
}
</script>
Run Code Online (Sandbox Code Playgroud)