gpt4 book ai didi

javascript - 如何更改 HTML.Twig 文件中隐藏文件的值

转载 作者:行者123 更新时间:2023-12-01 03:48:22 24 4
gpt4 key购买 nike

我的文件中有HTML.twig :

1:一个one隐藏字段应根据所选的 radio 取 3 个值,其中一个来 self 的数据库,另外两个值为 10。

<input type="hidden" name="lt_filed" value="{{ price.getLt }}" or "10" or "10">

2:3 个单选框

<input id="spa-price" name="price" class="w3-radio" onchange='valueLt();' value="Spare {{ price.getSparePrice }}" type="radio">
<input id="rep-price" name="price" class="w3-radio" onchange='valueLt();' value="Repair{{ price.getRepairPrice }}" type="radio">
<input id="tes-price" name="price" class="w3-radio" onchange='valueLt();' value="Test {{ price.getTestPrice }}" type="radio">

3:我在同一个文件 HTML.Twig 中做了一个 JavaScript block ,我创建了一个函数来获取每个 radio 的值,如下所示:

{% block javasc %}
<script>
function valueLt(){
var spare= document.getElementById('spa-price');
var repair= document.getElementById('rep-price');
var test= document.getElementById('tes-price');

var hidden_filed = document.getElementsByName("lt_filed");


if (repair.checked){ // Should take the value 10
alert("repair checked");
hidden_filed.value = 10;

} else if (test.checked){ // Should take the value 10 also
alert("test checked");
hidden_filed.value = 10;

} else {
alert("spare checked"); // should take the value from my database
hidden_filed.value = {{ price.getLt }};// Here I can't recovred the value from the Database

}
}
</script>
{% endblock %}

你能告诉我如何获得值{{ price.getLt }}通过 Twig 或 javascript ?

谢谢。

最佳答案

嗯,document.getElementsByName(param) 函数不返回元素实例,它返回一个元素数组。您应该选择第一个元素:

{% block javasc %}
<script>
function valueLt(){
var spare= document.getElementById('spa-price');
var repair= document.getElementById('rep-price');
var test= document.getElementById('tes-price');

var hidden_filed = document.getElementsByName("lt_filed")[0]; // CHANGE THIS LINE

if (repair.checked){ // Should take the value 10
alert("repair checked");
hidden_filed.value = 10;

} else if (test.checked){ // Should take the value 10 also
alert("test checked");
hidden_filed.value = 10;

} else {
alert("spare checked"); // should take the value from my database
hidden_filed.value = {{ price.getLt }};// Here I can't recovred the value from the Database

}
}
</script>
{% endblock %}

关于javascript - 如何更改 HTML.Twig 文件中隐藏文件的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43413276/

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