[英]two or more values in one row of selectbox javascript
In a form I need to put 2 values for each row of a select input like here: 在一种形式中,我需要为select输入的每一行添加2个值,如下所示:
<select name="amount" id="txtAmount" class="sbox" style="width: 260px;">
<option value="34.00" kredit="3000">30.00 kreditov = 34.00 € (1.13 €/kredit)</option>
<option value="19.75" kredit="1660">16.60 kreditov = 19.75 € (1.19 €/kredit)</option>
<option value="48.00" kredit="5000">50.00 kreditov = 48.00 € (0.96 €/kredit)</option>
</select>
Now I want to get kredit value with PHP: 现在我想用PHP获得kredit的价值:
echo 'kredit: '.$_REQUEST['???.kredit']';
echo 'amount: '.$_REQUEST['amount']';
The amount works, but kredit does not works. 该金额有效,但kredit无效。
How can I get kredit value in amount selectbox? 如何在金额选择框中获取kredit值?
You can't. 你不能 name
property of select
tag means the name of a query parameter, and value
of option
tag is the value. name
的属性select
标签意味着查询参数的名称和value
的option
标签值。 Store the kredit
values in your server. 将kredit
值存储在服务器中。
You cannot submit two values via the option tag as the value
attribute is the only value that will be submitted. 您不能通过选项标签提交两个值,因为value
属性是将要提交的唯一值。
With this said, you may not even want to do so. 这样说,您甚至可能不想这样做。 From what I can tell, the $kredit
variable holds the value of the users credit. 据我所知, $kredit
变量保存着用户信用的价值。 This could easily be modified client side and allow for additional credit to be set. 这很容易在客户端进行修改,并允许设置其他功劳。
Nevertheless, you could place both values within the value
attribute, with a delimiter to separate the values. 不过,您可以将两个值都放在value
属性中,并使用定界符将值分开。
<option value="<?php echo $amount .'-' . $kredit; ?>">Display name</option>
This would require you to separate them once the form is posted. 表单发布后,您需要将它们分开。
$values = explode('-', $_POST['amount']);
echo $values[0]; // Amount
echo $values[1]; // Kredit
The other option would be a Javascript
event that would populate another hidden element when the user selects the required option. 另一个选项是一个Javascript
事件,当用户选择所需的选项时,该事件将填充另一个隐藏的元素。
How about adding a hidden input
field with name='kredit
'? 如何添加一个name='kredit
'的隐藏input
字段?
<input type='hidden' name='kredit' id='kredit'/>
The hidden kredit field will be submitted to the server. 隐藏的kredit字段将被提交到服务器。
var select = document.getElementById('txtAmount');
select.addEventListener('change', function(e) {
var kredit = this.options[this.selectedIndex].getAttribute('kredit');
document.getElementById('kredit').value = kredit;
alert(document.getElementById('kredit').value);
});
This allows you to get the selected option's kredit attribute using: 这使您可以使用以下方法获取所选选项的kredit属性:
$_REQUEST['kredit']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.