如何根据选择的收音机更改隐藏文件的值 [英] How I change a value of hidden file according which radio selected

查看:37
本文介绍了如何根据选择的收音机更改隐藏文件的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 file HTML.twig 中有:

1- 一个隐藏文件应该根据选择的收音机取 3 个值,一个来自我的数据库,另外两个隐藏文件取值为 10,如下所示:

1- a hidden file should take 3 values according which radio selected, one from my database and two others hiden files values the number 10, like this:

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

2- 我有 3 个单选框:

2- And I have 3 radioBox:

<input id="spa-price" name="price" class="w3-radio" value="Spare {{ price.getSparePrice }}" type="radio">
<input id="rep-price" name="price" class="w3-radio" value="Repair{{ price.getRepairPrice }}" type="radio">
<input id="rep-price" name="price" class="w3-radio" value="Test {{ price.getTestPrice }}" type="radio">

3- 我在同一个文件 HTML.Twig 中做了一个块 Javascript我创建了一个函数来获取每个收音机的值:

3- I did a block Javascript in the same file HTML.Twig I created a function to get the value of each radio:

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

          if (repair.checked){ // Should take the value 10
              alert("repair checked");
              //<input type="hidden" name="lt" value="10">

          } else if (test.checked){ // Should take the value 10
             alert("test checked");
             //<input type="hidden" name="lt" value="10">

          } else {
             alert("spare checked"); // should take the value from DB
            // <input type="hidden" name="lt" value="{{ price.getLt }}">
        }
     </script>
{% endblock %}

你能告诉我如何根据函数中选择的每个收音机更改隐藏文件的值吗?谢谢.

Can you tell me how I change the value of my hidden file according each radio selected in the function ? Thank you.

推荐答案

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
{% block content %}
<input type="hidden" name="lt_filed" id="lt_filed" value="{{ price.getLt }}">

<input type="radio" name="price" value="{{ price.getSparePrice() }}">
<input type="radio" name="price" value="{{ price.getRepairPrice() }}">
<input type="radio" name="price" value="{{ price.getTestPrice() }}">
{% endblock %}

{% block javascript %}
<script>
$(function() {
	$('input[name="price"]').on('change', function() {
		$('#lt_filed').val($(this).val());
		console.log('Val set '+ $('#lt_filed').val());
	});
});
</script>
{% endblock %}

这篇关于如何根据选择的收音机更改隐藏文件的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆